I ended up with this solution:

I added this to resources/META-INF/spring.factories:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
org.mycasconfig.TicketRegistryConfiguration

I added this to ect/cas/config/cas.properties:
spring.main.allow-bean-definition-overriding=true
spring.autoconfigure.exclude=
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,org.apereo.cas.config.JpaTicketRegistryConfiguration
spring.component-scan.base-packages=org.mycasconfig
And here is the Configuration file that overrides and provides the 
EntityManager:
package org.mycasconfig;

import com.zaxxer.hikari.HikariDataSource;
import java.time.Duration;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.
EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.
EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration("ticketRegistryConfiguration")
@EnableTransactionManagement
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class TicketRegistryConfiguration {

private static final Logger LOGGER = LoggerFactory.getLogger
(TicketRegistryConfiguration.class);

@Bean(name = "ticketDataSource")
public DataSource ticketDataSource(final CasConfigurationProperties 
casProperties) {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setSchema(casProperties.getTicket().getRegistry().getJpa().getDefaultSchema());
dataSource.setJdbcUrl(casProperties.getTicket().getRegistry().getJpa().getUrl());
dataSource.setUsername(casProperties.getTicket().getRegistry().getJpa().getUser());
dataSource.setPassword(casProperties.getTicket().getRegistry().getJpa().getPassword());
dataSource.setDriverClassName(casProperties.getTicket().getRegistry().getJpa().getDriverClass());
dataSource.setMaximumPoolSize(casProperties.getTicket().getRegistry().getJpa().getPool().getMaxSize());
dataSource.setMinimumIdle(casProperties.getTicket().getRegistry().getJpa().getPool().getMinSize());
dataSource.setMaxLifetime(Duration.parse
(casProperties.getTicket().getRegistry().getJpa().getPool().getMaximumLifetime()).toMillis());
dataSource.setAutoCommit(casProperties.getTicket().getRegistry().getJpa().isAutocommit());
LOGGER.info("Ticket DataSource configured with URL: {}", 
dataSource.getJdbcUrl()); // Done
return dataSource;
}

@Bean(name = "ticketRegistryJpaVendorAdapter")
public HibernateJpaVendorAdapter ticketRegistryJpaVendorAdapter(final 
CasConfigurationProperties 
casProperties) {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabasePlatform(casProperties.getTicket().getRegistry().getJpa().getDialect());
adapter.setGenerateDdl(casProperties.getJdbc().isGenDdl());
adapter.setShowSql(casProperties.getJdbc().isShowSql());
LOGGER.info("TicketRegistry JpaVendorAdapter configured with dialect {}", 
adapter.getJpaDialect()); // Done
return adapter;
}

@Bean(name = "ticketEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean 
ticketEntityManagerFactory(Environment 
environment,
final CasConfigurationProperties casProperties,
@Qualifier("ticketDataSource") DataSource ticketDataSource,
@Qualifier("ticketRegistryJpaVendorAdapter") HibernateJpaVendorAdapter 
jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean emf = new 
LocalContainerEntityManagerFactoryBean();
emf.setDataSource(ticketDataSource);
emf.setPackagesToScan("org.apereo.cas.ticket");
emf.setJpaVendorAdapter(jpaVendorAdapter);
emf.setPersistenceUnitName("jpaTicketRegistryContext");
emf.getJpaPropertyMap().put("hibernate.dialect", 
casProperties.getTicket().getRegistry().getJpa().getDialect());
emf.getJpaPropertyMap().put("hibernate.hbm2ddl.auto", 
casProperties.getTicket().getRegistry().getJpa().getDdlAuto());
emf.getJpaPropertyMap().put("hibernate.show_sql", environment.getProperty(
"hibernate.show_sql", Boolean.class, false));
emf.getJpaPropertyMap().put("hibernate.format_sql", environment.getProperty(
"hibernate.format_sql", Boolean.class, false));

// Ensure initialization
emf.afterPropertiesSet(); // Force initialization to catch errors early
LOGGER.info("Ticket EntityManagerFactory configured with DataSource: {}", 
ticketDataSource); // Done
return emf;
}

@Bean(name = "ticketEntityManagerFactoryInstance")
public EntityManagerFactory ticketEntityManagerFactoryInstance(
@Qualifier("ticketEntityManagerFactory") 
LocalContainerEntityManagerFactoryBean emfBean) {
EntityManagerFactory emf = emfBean.getObject();
if (emf == null) {
LOGGER.error("EntityManagerFactory is null! Check DataSource and JPA 
configuration.");
throw new IllegalStateException("Failed to initialize EntityManagerFactory 
for JpaTicketRegistry");
}
LOGGER.info("ticketEntityManagerFactoryInstance successfully created: {}", 
emf);
return emf;
}

@Bean
public JpaTransactionManager transactionManager(
@Qualifier("ticketEntityManagerFactoryInstance") EntityManagerFactory 
entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
LOGGER.info("TransactionManager configured with EntityManagerFactory: {}", 
entityManagerFactory);
return transactionManager;
}

}

On Tuesday, March 18, 2025 at 10:16:37 PM UTC-5 Trent Edwards wrote:

> Has anybody found a solution for this? I'm on CAS 7.0.10 and I have this 
> same issue.
>
> On Thursday, February 29, 2024 at 9:50:23 AM UTC-6 Andrea Colajacomo wrote:
>
>> Ok,
>> so we are in two in searching for a solution
>> 😟
>>
>> Il giorno gio 29 feb 2024 alle ore 15:52 Mitchell Bösecke <
>> [email protected]> ha scritto:
>>
>>> Unfortunately no I have not solved this.
>>>
>>> On Thu, Feb 29, 2024, 7:51 AM Andrea Colajacomo <
>>> [email protected]> wrote:
>>>
>>>> Hello Mitchell,
>>>> had you solved this issue?
>>>>
>>>> It seems to be still present also on version 7
>>>>
>>>> Bye
>>>> Andrea
>>>>
>>>>
>>>> Il giorno venerdì 20 gennaio 2023 alle 07:20:56 UTC+1 Mitchell Bösecke 
>>>> ha scritto:
>>>>
>>>>> Is there no bug tracker for this project to report bugs??
>>>>>
>>>>> I found an incompatibility between libraries. The addition of 
>>>>> ""org.apereo.cas:cas-server-support-pm-jdbc"" will prevent you from also 
>>>>> adding *either *of the following:
>>>>>
>>>>>    - "org.apereo.cas:cas-server-support-events-jpa"
>>>>>    - "org.apereo.cas:cas-server-support-jpa-ticket-registry"
>>>>>    
>>>>> In both cases you get a stack trace during application startup saying 
>>>>> the following:
>>>>>
>>>>> Caused by: java.lang.ClassCastException: class 
>>>>> org.springframework.beans.factory.support.NullBean cannot be cast to 
>>>>> class 
>>>>> javax.persistence.EntityManagerFactory 
>>>>> (org.springframework.beans.factory.support.NullBean and 
>>>>> javax.persistence.EntityManagerFactory are in unnamed module of loader 
>>>>> org.springframework.boot.loader.LaunchedURLClassLoader @277050dc)
>>>>>
>>>>> *To replicate:*
>>>>>
>>>>> I start a new project using the CAS Initializer and version 6.6.4:
>>>>>
>>>>> mkdir cas-test
>>>>> cd cas-test
>>>>> curl -k https://casinit.herokuapp.com/starter.tgz -d casVersion=6.6.4 
>>>>> -d dependencies="core" | tar -xzvf -
>>>>>
>>>>> Add the following into the gradle.build file within the "dependencies" 
>>>>> section:
>>>>>
>>>>> implementation "org.apereo.cas:cas-server-support-pm-webflow"
>>>>> implementation "org.apereo.cas:cas-server-support-pm-jdbc"
>>>>>
>>>>> implementation "org.apereo.cas:cas-server-support-jpa-ticket-registry"
>>>>>
>>>>> And update /etc/cas/config/cas.properties to have some basic setup:
>>>>>
>>>>> server.port=8444
>>>>>
>>>>> cas.ticket.registry.jpa.driver-class=org.postgresql.Driver
>>>>>
>>>>> cas.ticket.registry.jpa.dialect=org.hibernate.dialect.PostgreSQL10Dialect
>>>>> cas.ticket.registry.jpa.enabled=true
>>>>> cas.ticket.registry.jpa.user=redacted
>>>>> cas.ticket.registry.jpa.password=redacted
>>>>> cas.ticket.registry.jpa.url=jdbc:postgresql://redacted
>>>>>
>>>>> Try to startup the application and it fails. If you remove the 
>>>>> "support-pm-jdbc" module from the gradle file, it will startup just fine.
>>>>>
>>>>

-- 
- Website: https://apereo.github.io/cas
- List Guidelines: https://goo.gl/1VRrw7
- Contributions: https://goo.gl/mh7qDG
--- 
You received this message because you are subscribed to the Google Groups "CAS 
Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/a/apereo.org/d/msgid/cas-user/62e40275-30bd-4dec-ac20-ab3baa88fcf6n%40apereo.org.

Reply via email to