configure hibernate to map entities automaticly - java

I use hibernate ORM in my project.
Now I map entities like this :
<mapping class="entities.User"/>
but I have to do this for each entity I create - is there anything I can put in the hibernate configuration to make it scan itself for annotated entities in certain package?
thank you

You can place all your java entities in a JAR file and then provide the path of the JAR file in hibernate configuration file like this:
<mapping jar="path_to_your_jar_file"/>
Update:
This is helpful only if you have hbm.xml files for mapping instead of having annotations on your classes. These mapping files should be part of the JAR file.
Look at this link for addJar method of Configuration class.

Using spring can help you get the package scanned. See the config below
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>com.tds.hibernate.entities</value>
</list>
</property>
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>

Related

Spring repository sends HQL to database server

I have a simple spring data repository which extends CrudRepository
Also I configured
<context:component-scan base-package="by.acme.vt.*"/>
and
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="eqDataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<!-- Set JPA properties -->
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="javax.persistence.schema-generation.database.action">none</prop>
</props>
</property>
<!-- Set base package of your entities -->
<property name="packagesToScan">
<array>
<value>by.acme.vt.asdint</value>
<value>by.acme.vt.asd.domain</value>
<value>by.acme.vt.asd.repository</value>
</array>
</property>
</bean>
and
<jpa:repositories base-package="by.acme.vt.*" entity-manager-factory-ref="entityManagerFactory"/>
So
by.acme.vt.asdint
belongs to the project which I am working on but packages
<value>by.acme.vt.asd.domain</value>
<value>by.acme.vt.asd.repository</value>
belongs to the included jar file
If I create the same repo in the project, it works fine but for repositories which were included in jar, even when I run a simple code like
myrepo.findById(id)
It sends a pure HQL to the server, so it does not transform it to SQL at all.
I see response from mssql like.
table MyUser does not exist on server.
But it supposed to use my_user table but not MyUser

How to specify relative path for hibernate.javax.cache.uri property in xml based spring configuration

I have legacy spring application which is built using xml based configuration I am trying to configure session factory with second level cache.
I have ecache.xml file present in resource folder also hibernate.javax.cache.uri property expects absolute path for ecache.xml.
if i provide URI as file:///c:/App/resources/ecache.xml it works. But this is not good for deployments, maintenance.
how can specify relative path like classpath:ehcache.xml or /WEB-INF/ehcahe.xml in spring xml based configurations ?
Note: I am not using spring boot.
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.example.vl.model</value>
</list>
</property>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.javax.cache.uri">file:///c:/App/resources/ecache.xml</prop>
</props>
</property>
</bean>
OK, I am able to get absolute uri in xml based configuration using Spring EL As follows.
<prop key="hibernate.javax.cache.uri">#{ new org.springframework.core.io.ClassPathResource("/ehcache.xml").getURI().toString()}</prop>

Do I need to register all the model class in hibernate config file

Suppose I have number of model classes(Entity class). Do I need to register all the model class in hibernate config file one after another like
...
<mapping class="com.java.ent.Table"/>
...
or any annotation is there which marks as entity? For medium app there would be huge amount of table and its corresponding model entity. how to manage it?
There another way to configure hibernate sessionFactory where you can actually give only packageToScan.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.connection.useUnicode">true</prop><!-- added -->
<prop key="hibernate.connection.characterEncoding">UTF-8</prop><!-- added -->
<prop key="hibernate.connection.charSet">UTF-8</prop><!-- added -->
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.web.entities</value>
</list>
</property>
</bean>
You can write your own set package in AnnotationConfiguration as described in the dicumentation.
https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/ch01.html
Or
Another option is to write custom AnnotationConfigurationWithWildcard which extends the org.hibernate.cfg.AnnotationConfiguration and inject as spring dependency.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfigurationWithWildcard"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
You can use which ever suits you better.

Configuring JPA in Spring application

I am configuring Spring to use JPA by using Hibernate implementation. However I don't understand the process completly. I have gotten it to work by following different blogs etc. I have used EJB 3.1 and there I had a persistence.xml. However in spring I declared a LocalContainer...Bean and provided some properties to it, and I have no persistence.xml. Could someone explain how it works in Spring and what the declared bean is?
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="com.company.domain" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.PostgreSQL82Dialect
</prop>
</props>
</property>
</bean>
There are different flavors of Spring Configuration with JPA, one which requires persistence.xml and other which requires just bean declarations(no persistence.xml).
I am going to take up the Case-2 in your scenario:
The main reasons we want a persistence.xml is because of the following reasons:
Database connectivity details.
Java classes which are treated as Entities or packages in which to scan for Entities.
Other vendor specific settings like hibernate.show_sql or similar stuff.
Now if spring provides a way to mention all this together in bean configurations then there is no need to have the persistence.xml.
In case of your bean definitions, lets break it down.
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="com.company.domain" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.PostgreSQL82Dialect
</prop>
</props>
</property>
</bean>
First property, dataSource already contains the database settings.
Second property, jpaVendorAdapter is a property specific to Spring
Third property, packagesToScan this is a property of Spring to scan for entities, this we either do in persistence.xml by using "class" tags by mentioning each class FQN.
Fourth property, jpaProperties as the name suggests can either be in Spring or in persistence.xml
e.g.
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
Since you have all the configurations already in Spring bean, there's no need to have a persistence.xml
Just to add a FootNote:
Spring 3.1 provides an alternative: LocalContainerEntityManagerFactoryBean that accepts a 'packagesToScan' property, specifying base packages to scan for #Entity classes.
Hope this answer your queries.

Spring SessionFactory creation domain objects auto scanning

In my spring dao configuration xml I currently have to manually list out the domain classes names. Is there any way to automate this to eliminate the need to manually list out a domain class whenever a new one is created?
To give a better idea of what I want to do this, using something similar to component-scan or such
Current code
<bean id="daoSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="applicationDataSource" />
<property name="annotatedClasses">
<list>
<value>com.greenwhich.application.domain.Driver</value>
<value>com.greenwhich.application.domain.DriverRealTimeCurrentLocation</value>
<value>com.greenwhich.application.domain.Journey</value>
<value>com.greenwhich.application.domain.Customer</value>
<value>com.greenwhich.application.domain.SystemConstants</value>
<value>com.greenwhich.application.domain.DriverRequest</value>
<value>com.greenwhich.application.domain.Account</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
All I require that the values under the "annotatedClasses" property is automatically detected
Is there any way to implement this? So far I have tried inserting a component-scan inside of the "annotatedClasses" property searching for the "Entity" annotation which did not work
Any help is greatly appreciated
You should be able to replace the annotatedClasses property with:
<property name="packagesToScan" value="com.greenwhich.application.domain" />
as part of your session factory configuration.

Categories

Resources