Entity Manager does not save data in the database

Through the entity manager, I try to save the object in the database, but I can not save it. Here is my configuration.

I have this Entity:

@Entity @Table(name = "User") public class UserModel implements Serializable, ModelItem { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(nullable = false) private String username; @Column(nullable = false) private String password; @Column(nullable = false) private String name; private String surname; private String notes; private String cellphone; @Column(nullable = false) private String email; private Boolean enabled; //get and set methods ..... } 

and my bean import, which makes persistence:

 @Repository public class ImportServiceImpl implements ImportService { @PersistenceContext protected EntityManager entityManager; @Transactional public boolean importExample() { User u= new User(); u.setUsername("username"); u.setPassword("password"); u.setName("name"); u.setEmail("email"); entityManager.persist(u); } } 

Spring configuration for entity manager and db communications:

 <tx:annotation-driven transaction-manager="transactionManager" /> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <tx:annotation-driven /> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <!-- Prints used SQL to stdout --> <property name="generateDdl" value="true" /> <!-- Generates tables. --> <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" /> </bean> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url"> <value>${db.url}</value> </property> <property name="username"> <value>${db.username}</value> </property> <property name="password"> <value>${db.password}</value> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> <property name="dataSource" ref="dataSource"/> </bean> 

and my persistence.xml

 <?xml version="1.0" encoding="UTF-8"?> <persistence 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_1_0.xsd" version="1.0"> <persistence-unit name="application" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/testdata"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.password" value="password"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties> </persistence-unit> </persistence> 

Therefore, when I run my example, I get no errors, but the entity is not saved. I also tried adding entityManager.flush () after saving, but in this case I get this error:

 javax.persistence.TransactionRequiredException: no transaction is in progress at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:793) 

So, I think my Transactional bean is not very attached to the method, but I can not understand the reason. Does anyone know why?

I also noticed in STS that 2 beans with the same data are generated for this transaction, this looks strange (I don't know if this is an STS error or is it a problem in my configuration that creates 2 beans):

enter image description here

+4
source share
5 answers

I ran into a problem similar to that described. I found the problem in misusing @Transactional notation, in particular, I used javax.transaction.Transactional incorrectly instead of org.springframework.transaction.annotation.Transactional

A detailed description of the difference can be found here in javax.transaction.Transactional vs org.springframework.transaction.annotation.Transactional

+4
source

Maybe try to specify the name of the persistence unit of measure "application" in the annotation @PersistenceContext?

@PersistenceContext (unitname = "application")

+1
source

I also ran into the same problem as Repoker said the Persistence provider is available but my transaction manager was screwed up. I added transactionManager bean to my application-config.xml file, like this

 <bean id="transactionManager" cass="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="myEntityManagerFactory" /> </bean> 

And now it works great!

+1
source

you can use entityManager.flush() right after saving and merging, this can solve your problem.

0
source

You have <tx:annotation-driven /> twice in your context configuration. I'm not sure if this is a legit config.

0
source

All Articles