4

Trough the entity manager I'm tryng to persist the entity in the database but I don manage to persists it. Here goes 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 import bean that does the 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);
    }
}

The spring configuration for the entity manager and the db connction:

<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>

So when I run my example I don't get any error but entity is not persisted. I also tryied to add entityManager.flush() after persist 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'm thinking my Transactional bean is not well binded to the method, but I can not understand the reason. Somebody knows why?

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

enter image description here

ddelizia
  • 1,571
  • 5
  • 30
  • 54
  • Have you tried with `@Transactional(propagation = Propagation.REQUIRED)` ? – davioooh Jul 06 '12 at 07:43
  • Still does not work I get the same behaviour... – ddelizia Jul 06 '12 at 07:53
  • I tested the entityManager.find(...) after inserting manually data into the db and it works, but the persist does not save data... Why? :( – ddelizia Jul 06 '12 at 11:28
  • Hello! How did you managed to solve this problem? I'm facing a similar issue described here: http://stackoverflow.com/questions/17643456/spring3-jpa-hibernate4-not-persisting I apreciate any help. Thanks – Marcelo Jul 15 '13 at 01:25

6 Answers6

5

I've faced an issue similar to the one described. I found the problem in incorrect usage of @Transactional notation, in particular I've wrongly used javax.transaction.Transactional instead of org.springframework.transaction.annotation.Transactional

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

Community
  • 1
  • 1
lorenzo
  • 61
  • 1
  • 4
1

Maybe try specifying the persistence unit name "application" in your @PersistenceContext annotation?

@PersistenceContext(unitname = "application")

Repoker
  • 202
  • 3
  • 12
1

I also faced the same issue, as Repoker said Persistence provider is ok but my transaction manager was screwed. I added transactionManager bean in my application-config.xml like this

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

and It works fine now!!

Kevindra
  • 1,682
  • 3
  • 24
  • 45
0

you can use entityManager.flush() just after persist and merge it could solve your problem

henrycharles
  • 1,019
  • 6
  • 28
  • 66
0

You have <tx:annotation-driven /> twice in your context config. I'm not sure that is a legal config.

Andreas
  • 4,937
  • 2
  • 25
  • 35
0

I had a similar issue where @Transactional didn't persist entity in the DB using EntityManager. To solve this problem we can commit the transaction manually.

entityManager.getTransaction().begin();
entityManager.persist(post);
entityManager.getTransaction().commit();
jsmin
  • 372
  • 2
  • 12