I've got this persistence problem, I can't manage to persist my Entities to my MySQL database.
I'm working with Spring+Hibernate and the JpaTransactionManager with an EntityManagerFactory.
When I call the
EntityManager.persist("entity"); I get this log result :
10:57:32,828DEBUG SharedEntityManagerCreator$SharedEntityManagerInvocationHandler:184 - Creating new EntityManager for shared EntityManager invocation
10:57:32,828DEBUG SessionImpl:220 - opened session at timestamp: 12454018528
10:57:32,937DEBUG AbstractSaveEventListener:307 - delaying identity-insert due to no transaction in progressI've found other people with similar problems but never managed to fix mine. I suppose it's due to my transactions.
here are my configuration files :
hibernate.propertiesCode:
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.c3p0.min_size=1
hibernate.c3p0.max_size=10
hibernate.c3p0.timeout=0
hibernate.c3p0.max_statements=5
hibernate.c3p0.idle_test_period=3000
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.generate_statistics=true
hibernate.hbm2ddl.auto=create-drop
hibernate.hbm2ddl.export=true
hibernate.hbm2ddl.delimiter=type=InnoDB
hibernate.transaction.factory_class=org.hibernate.transaction.JDBCTransactionFactory
hibernate.current_session_context_class=thread
application-context-ressources.xmlCode:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost/ecommerce"
p:username="xxx"
p:password="xxx"/>
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultDataSource" ref="dataSource"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource">
<property name="persistenceUnitManager" ref="persistenceUnitManager"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="MYSQL"
p:showSql="true"
p:generateDdl="true"
/>
</property>
</bean>
</beans>
application-context.xmlCode:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.mvnCommerce.base" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
</beans>
Here are the code files :
the Entity inherits an ID from entity base
User.javaCode:
package com.mvnCommerce.web.model;
import com.mvnCommerce.base.model.EntityBase;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "USERS")
public class User extends EntityBase{
@Column(name = "NAME")
private String name;
@Column(name = "PASSWORD")
private String password;
@Column(name = "AGE")
private int age;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "ADRESS")
private Set<Adress> adresses;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "BILLING_DETAILS")
private Set<BillingDetails> billingDetails;
public User() {
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return this.name;
}
public String getPassword() {
return this.password;
}
public int getAge() {
return this.age;
}
}
the service that calls the DAO
userServiceImpl.javaCode:
package com.mvnCommerce.web.service.impl;
import com.mvnCommerce.web.service.*;
import com.mvnCommerce.web.dao.UserDao;
import com.mvnCommerce.web.dto.UserDto;
import com.mvnCommerce.web.dto.Utils.UserDtoUtils;
import com.mvnCommerce.web.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class UserServiceImpl implements UserService {
private UserDao userDao;
private UserDtoUtils userDtoUtils;
@Autowired
public void setUserDtoUtils(UserDtoUtils userDtoUtils) {
this.userDtoUtils = userDtoUtils;
}
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public UserDto createUser(UserDto user) throws Exception {
User u = userDtoUtils.dtoToEntity(new User(),user);
userDao.addUser(u);
//Add code to send an email to the new user
return userDtoUtils.ENTITY_TO_DTO.transform(u);
}
}
the Dao
UserDaoImpl.javaCode:
package com.mvnCommerce.web.dao.jpa;
import com.mvnCommerce.web.dao.*;
import com.mvnCommerce.web.model.User;
import com.mvnCommerce.base.dao.jpa.DaoBaseImpl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
*
* @author Usuario
*/
@Repository
public class UserDaoImpl extends DaoBaseImpl<User> implements UserDao {
public UserDaoImpl() {
super();
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void addUser(User user){
getEntityManager().persist(user);
}
@Override
public boolean userExists(User user){
try{
getById(user.getId());
}catch(Exception e){
return false;
}
return true;
}
}
userService.createUser(user); this method is call in my controller.
This is an amount of code I hope sombody can help me this is my first hibernate project and I'm only making test, but I don't manage the easiest :(
Thanks a lot in advance.