LazyLoadingException appears when I try to get an object from ManyToOne relation - java

Here's my entity:
#Entity
public class Answer implements Serializable {
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="user")
private CoreUsers user;
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="person")
private Person person;
}
the service:
#Transactional
public class AnswerService {
#Autowired
private AnswerRepository repository;
public List<Answer> getAnswers(Long id) {
return repository.findByMessagesidOrderBySenddateDesc(id);
}
}
the repo:
#Repository
public interface AnswerRepository extends JpaRepository<Answer, Long> {
List<Answer> findByMessagesIdOrderBySenddateDesc(Long id);
}
the applicationContext.xml
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<entry key="hibernate.enable_lazy_load_no_trans" value="true" />
<entry key="hibernate.current_session_context_class" value="thread" />
<entry key="org.hibernate.cacheable" value="false" />
</map>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="showSql" value="false" />
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
</bean>
</property>
<property name="persistenceUnitName" value="cccPersistenceUnit" />
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
</bean>
I load a collection of Answer (List) in the JSF bean through my service class. When I try to obtain a Person object from the collection — everything is okay, but when I try to obtain a User object from the collection — hibernate throws LazyInitializationException. What am I doing wrong?
Am I right, that hibernate tries to obtain the user object out of hibernate session?
Pls feel free to ask any addition information.
Thanks for any helps!
before create the thread, I googled many times
UPD.
I set the entities up to Eager loading, and noticed that my app tries to load unexist ID from user table. That was the problem :)

You can get your complete objects tree with :
import org.springframework.transaction.annotation.Transactional;
#Transactional
public List<Answer> getAnswers(Long id) {
return repository.findByMessagesidOrderBySenddateDesc(id);
}

Related

Inject multiple datasources to single EntityManagerFactory?

I am trying to fetch data from two different tables of two different schemas (logical db) in same database server using innerjoin query +JPA nativesql. How can I inject multiple datasources to same entity manager?
my config file looks like this
<bean id="userDataSource" class="org.jdbcdslog.DataSourceProxy">
<description>Data source for User database</description>
<property name="targetDSDirect">
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/cUser" />
<property name="resourceRef" value="true" />
</bean>
</property>
</bean>
<bean id="masterDataSource" class="org.jdbcdslog.DataSourceProxy">
<description>Data source for User database</description>
<property name="targetDSDirect">
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/Master" />
<property name="resourceRef" value="true" />
</bean>
</property>
</bean>
<bean id="entitymanager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation"
value="classpath:com/jpa_persistence.xml" />
<property name= "persistenceUnitName" value= "CP"/>
<property name="dataSource" ref="userDataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="jpaPropertyMap">
<map>
<entry key="eclipselink.weaving" value="false"/>
</map>
</property>
</bean>
Most of the database engines i know do not required these kind of evil double datasource tricks, you can just grant read (or write) access to both schema on the same user.
This way the user will have access to both of those schemas and be able to cross query.
Finally use the Entity anotation to define which schema to use
#Entity
#Table(name = "author", schema = "bookstore")
public class Author { ... }

Unable to Inject DAO class in Spring BO [duplicate]

This question already has answers here:
Why is my Spring #Autowired field null?
(21 answers)
Closed 7 years ago.
I am unable to inject my DAO class in Spring MVC:
This is my DAO class:
package com.pankaj.bookslibrary.dao;
#Component
public class BooksLibraryDAO
{
#PersistenceContext
private EntityManager em;
public void saveBook(Book book)
{
em.persist(book);
}
}
This is my BO class which calls DAO:
package com.pankaj.bookslibrary.controller;
#Service
public class BooksLibraryBO
{
#Autowired
private BooksLibraryDAO booksLibraryDAO;
public void saveBook(Book book)
{
booksLibraryDAO.saveBook(book);
}
The above line gives NullPointerException as booksLibraryDAO is null.
Here are the relevant lines from my dispatcherServlet config file:
<beans xmlns=...3.0.xsd">
<context:component-scan base-package="com.pankaj.bookslibrary" />
<context:annotation-config/>
<bean id="dataSource"....</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="BooksLibrary_PersistenceUnit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="showSql" value="false" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
<bean id="transactionManagerNonJTA" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="defaultTimeout" value="1800"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManagerNonJTA" />
I am not sure what I have missed.
This is how I am making a call from the controller:
BooksLibraryBO bo = new BooksLibraryBO();
bo.saveBook(book);
The packages the classes are in differs from the package you scan for annotations. Add the packages to the list of base-packages to scan for!
As M.Deinum explained, you are creating the BooksLibraryBO yourself, spring do not know that instances and will not process the annotations.

Spring/JPA/Hibernate persist entity : Nothing happen

I'm trying to make an application with Spring 3, JPA 2 and Hibernate 3.
I have a problem when y persist an entity : nothing happen ! Data are not inserted in database, and not query is executed.
But when i'm using a request like query.getResultList() a select works correctly.
So I think my problem is only on a persist/update and on the transaction manager but i'm not really good with spring.
Can you help me please ?
Here are my configuration files :
My applicationContext.xml
<jee:jndi-lookup id="soireeentreamis_DS" jndi-name="jdbc/soireeentreamis" />
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="soireeentreamis_DS" />
<property name="dataSources">
<map>
<entry key="soireeentreamisDS" value-ref="soireeentreamis_DS" />
</map>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="soireeentreamisPU" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
</bean>
</property>
</bean>
<bean id="soireeentreamisTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<tx:annotation-driven transaction-manager="soireeentreamisTransactionManager" />
<context:annotation-config />
</beans>
My persistence.xml
<persistence-unit name="soireeentreamisPU"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>soireeentreamisDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
My service
#Service
#Transactional("soireeentreamisTransactionManager")
public class UserServiceImpl implements UserService ...
My dao
#Repository
public class UserDaoImpl extends GenericDaoImpl<User, String> implements
UserDao {
#PersistenceContext(unitName = "soireeentreamisPU")
private EntityManager em;
public void persist(final User entity) {
em.persist(entity);
}
}
Can somebody help me please?
I find similar problem a while ago. In my case I needed to add line below to my dispacher-servlet.xml. So I need this in 2 places (applicationContex.xml and dispacher-servlet.xml)
<tx:annotation-driven />
And to clear something out, you didn't show your service methode that "store" object, but I believe that it's annotated with #Transactional - cause wihout this one you want create new transaction.
This happened to me recently. The problem is as you say a transactional problem, add the #Transactional annotation to all the methods that update the DB. Then add the CGLIB library to your calsspath or add it to your pom.xml if you are using Maven, this is necessary for spring make transactions. Even if I did it on a different way I hope it can help you. Here is my db.xml where I have all the data base related spring configuration.
<!-- Scans within the base package of the application for #Components to configure as beans -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="${db.dialect}" />
</bean>
</property>
</bean>
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClass" value="${db.driver}" />
<property name="jdbcUrl" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="idleConnectionTestPeriodInMinutes" value="1" />
<property name="idleMaxAgeInMinutes" value="4" />
<property name="maxConnectionsPerPartition" value="30" />
<property name="minConnectionsPerPartition" value="10" />
<property name="partitionCount" value="3" />
<property name="acquireIncrement" value="5" />
<property name="statementsCacheSize" value="100" />
<property name="releaseHelperThreads" value="3" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
And here is my persistence.xml
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="pu-app" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
You need to add the model object into the persistence.xml file. Right below the provider element add
<class>com.your.domain.object.User</class>

#Transactional in unit test prevents #PostLoad from being invoked

I have the following test:
public class Book
{
public boolean postLoadInvoked;
#PostLoad
private void onPostLoad()
{
postLoadInvoked = true;
}
}
public class MyIntegrationTest extends AbstractIntegrationTest
{
#Autowired
private BookDAO bookDAO;
#Test
public void loadBooks()
{
Book book = bookDAO.findOne(...);
assertTrue(book.postLoadInvoked);
}
}
This test passes as-is, but if I add the #Transactional annotations to the test class, it fails:
#Transactional
#TransactionConfiguration(defaultRollback=true)
public class MyIntegrationTest extends AbstractIntegrationTest
Why does configuring the test with #Transactional affect the JPA callback methods?
EDIT
The DAO is just a Spring Data repository, so has no logic:
public interface BookDAO extends
JpaRepository<Book, Long>,
QueryDslPredicateExecutor<Book> {}
The transaction manager also has a standard configuration:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.mangofactory.example</value>
</list>
</property>
<property name="persistenceUnitName" value="spring-test" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
</bean>
I'm not sure what the spring version is that you are using or if AbstractIntegrationTest is extending something, but one thing to check is that in your hierarchy your tests should extend some abstract transactional context aware spring test class.
The classes that I'm thinking about:
org.springframework.test.AbstractTransactionalSpringContextTests
org.springframework.test.AbstractTransactionalDataSourceSpringContextTests
or some jUnit flavor (depending on the version that you are using):
org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests
org.springframework.test.context.junit38.AbstractTransactionalJUnit38SpringContextTests
I could think of two reasons:
#PostLoad is only executed after the transaction is committed. Unlikely.
How did you enable transactions? Did you specify proxyTargetClass = true? Otherwise, you can get weird behavior.

Spring + EntityManagerFactory +Hibernate Listeners + Injection

i have a simple question. Its possible to add dependency injection via #Ressource or #Autowired to the Hibernate Eventlistener?
I will show you my entitymanagerfactory configuration:
<bean id="entityManagerFactory" class="org.hibernate.ejb.EntityManagerFactoryImpl">
<qualifier value="entityManagerFactory" />
<constructor-arg>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager">
<bean
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManagerr">
<property name="defaultDataSource" ref="dataSource" />
</bean>
</property>
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="mis" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
<property name="jpaProperties" ref="jpa.properties" />
<property name="jpaDialect" ref="jpaDialect" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="database">
<util:constant
static-field="org.springframework.orm.jpa.vendor.Database.POSTGRESQL" />
</property>
<property name="showSql" value="true" />
</bean>
</property>
</bean>
</constructor-arg>
</bean>
At the moment i register my listener via jpa.properties,
hibernate.ejb.event.load=com.example.hibernate.events.LoadEvent
but in this case i have no spring injection in my listener. I found a solution, but this use the sessionFactory and not the entitymanager oder can i modifiy the sessionfactory in my context? Hopefully someone have a nice idea or solutionhow to deal with this problematic!
Big thanks!
If you used SessionFactory, this would be the configuration:
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- Stripped other stuff -->
<property name="eventListeners">
<map>
<entry key="pre-load">
<bean class="com.mycompany.MyCustomHibernateEventListener1" />
</entry>
<entry key="pre-persist">
<bean class="com.mycompany.MyCustomHibernateEventListener2" />
</entry>
</map>
</property>
</bean>
But since you are using JPA, I'm afraid you need to use AOP as outlined in this thread
Or you can
store the ApplicationContext in a ThreadLocal or a custom holder class and expose it through a static method
have a base class for your listeners something like this:
Base class:
public abstract class ListenerBase{
protected void wireMe(){
ApplicationContext ctx = ContextHelper.getCurrentApplicationContext();
ctx.getAutowireCapableBeanFactory().autowireBean(this);
}
}
Now in your lifycycle methods call wireMe() first.
Update:
Here is a sample implementation of ContextHelper:
public final class ContextHelper implements ApplicationContextAware{
private static final ContextHelper INSTANCE = new ContextHelper();
private ApplicationContext applicationContext;
#Override
public void setApplicationContext(final ApplicationContext applicationContext){
this.applicationContext = applicationContext;
}
public static ApplicationContext getCurrentApplicationContext(){
return INSTANCE.applicationContext;
};
public static ContextHelper getInstance(){
return INSTANCE;
}
private ContextHelper(){
}
}
Wire it in your Spring Bean configuration like this:
<bean class="com.mycompany.ContextHelper" factory-method="getInstance" />

Categories

Resources