Configure data source in spring and mapping in hibernate - spring

Is it possible to configure to DataSource configuration in spring xml and keep the resource mapping in hibernate configuration file.

You can find an example in the Spring reference (http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#orm-hibernate).

Related

How to access datasource information in spring boot with spring data jpa and hibernate

I need to perform an health check on my application that uses spring boot with spring data jpa and hibernate.
I need to do this with jpa and not make specific to any implementation.
In EclipseLink we can use EntityManagerFactoryDelegate as shown below, but I dont know How to do this with spring data jpa and hibernate.
EntityManagerFactoryDelegate delegate = entityManager.getEntityManagerFactory().unwrap(EntityManagerFactoryDelegate.class);
PersistenceInfo = delegate.getSetupImpl().getPersistenceUnitInfo();
DataSourceImpl dataSource = (DataSource) info.getNpnJtaDataSource();
return dataSource.getName();
Can anyone suggest me how to do this in spring data jpa using hibernate.
You can use spring boot actuator dependency for healthcheck doesn't need to configure externally. Once you define Datasource Bean it will auto pick database healthcheck.
if you want to enable/disable database health check you can use the below property,
management.health.db.enabled=<boolean, true || false>
implementation reference :https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html

Set a Spring Boot property using a value from JNDI

How can I put a string stored in JNDI to Spring Boot properties?
A bit more details:
I get my DataSource from JNDI using this property in application.properties:
spring.datasource.jndi-name=my_data_source_jndi_name. Currently, the DB schema is hard coded in application.properties in the following way: spring.jpa.properties.hibernate.default_schema=my_schema. There is a JNDI record that containd the DB schema name. How can I get the schema from JNDI too?
As outlined here:
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
Spring will resolve properties from JNDI and so you can therefore parameterise it in various ways. You can bypass the properties file and use the #Value() annotation directly in Java config
#Value("my.property.in.jndi")
private String schema;
or you can do as below in the properties file:
spring.jpa.properties.hibernate.default_schema=${my.property.in.jndi}

Is there any bean created for spring #propertySource annotation

I have used spring #propertysource annotation to load properties file in my spring boot project . I just need to know is there any bean created for this annotation when application gets started
#PropertySource annotation is used to let spring-boot know the location of the properties required by your application. It will not create any bean.

Is it possible to use Spring boot with web.xml and annotation based configurataion

I want to use Spring Boot with web.xml and servlet 3.1 configuration.Is there any example?
I want to define my context(Dispatcher servlet/SpringBootServletInitializer) in Web.xml mean time define all other configuration using annotation based.Ex want to load application.properties/yml values using pojos.
Need this type of configuration to deploy the app in Liberty profile as Liberty expecting application context in web.xml when using liberety global sharelib.

SpringBoot DataSource configuration

I'm trying to use the application.properties file to configure the datasource Spring Boot will have to use.
I've put the following properties inside :
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.user=test
spring.datasource.password=test
spring.datasource.jdbcUrl=jdbc:postgresql://localhost:5432/test
The application.properties file is well used by other systems. But I can't get it to work for the automatic datasource configuration.
I'm still getting this Exception :
org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database url for database type NONE.
The postgresql driver is included and loaded. And I can configure the datasource using a Configuration class, and the same parameters as above.
I've also added the #EnableAutoConfiguration and #EnableJpaRepositories to my Application.class.
Any clues?
You should use spring.datasource.url to configure the JDBC URL rather than spring.datasource.jdbcUrl.
spring.datasource.jdbcUrl will work if the specific DataSource implementation that you're using has a setJdbcUrl method (HikariCP, for example) where as spring.datasource.url will work with any of the supported datasources.
Using spring.datasource.url also has the added benefit that you don't need to specify spring.datasource.driverClassName as it will be inferred from the url.

Resources