Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Hibernate persist does not result in insert

I'm trying to implement a simple DAO. I have a Dao:

@Repository("iUserDao")
@Transactional(readOnly = true)
public class UserDao implements IUserDao {
    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public User getById(int id) {
        return entityManager.find(User.class, id);
    }

    @Override
    public boolean save(User user) {
        entityManager.persist(user);
        entityManager.flush();
        return true;
    }

    @Override
    public boolean update(User user) {
        entityManager.merge(user);
        entityManager.flush();
        return true;
    }

    @Override
    public boolean delete(User user) {
        user = entityManager.getReference(User.class, user.getId());
        if (user == null)
            return false;
        entityManager.remove(user);
        entityManager.flush();
        return true;
    }

And an entity:

@Entity
@Table(name = "users")
public class User {
    private int id;
    private Date creationDate;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }

    public User() {
    }

    public User(Date creationDate) {
        this.creationDate = creationDate;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }
}

Here's appContext.xml: `

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter"
      p:persistenceUnitName="test">
    <property name="loadTimeWeaver">
        <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
      p:entityManagerFactory-ref="entityManagerFactory"/>
<bean id="jpaAdapter"
      class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
      p:database="MYSQL" p:showSql="true"/>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<tx:annotation-driven/>`

Unless I call flush() after persist() or merge() the insert in not executed. Why is that? If I remove @Transactional then I get error on "no transaction is in progress" on flush, but if remove flush nothing inserted to the DB.

like image 910
AlexV Avatar asked Mar 02 '11 14:03

AlexV


2 Answers

It works this way because you marked transaction as read only with @Transactional(readOnly = true).

As you can see, it doesn't make your transactions actually read-only since you still can persist changes by calling flush() manually. However, it disables automatic flush at the end of transaction, so that changes are not persisted without manual flush.

You need either remove readOnly from class-level annotation, or override it on non-read-only methods with method-level annotations:

@Override
@Transactional(readOnly = false)
public boolean save(User user) { ... }

Also note that transaction demarcation is usually applied to service layer methods, not to DAO methods. In particular, when writing DAO methods you actually don't know which transactions should be read-only, and which are not. This information is available only when designing service layer, as you can see in this example:

public class UserService {
    @Autowired UserDAO dao;

    @Transactional(readOnly = true) 
    public User getUserById(int id) {
        return dao.getById(id); // getById() can participate in effectively read-only transaction
    }

    @Transactional
    public void changeUserName(int id, String newName) {
        User u = dao.getById(id); // Or not
        u.setName(newName); // Change will be flushed at the end of transaction
    }
}
like image 93
axtavt Avatar answered Sep 22 '22 01:09

axtavt


Try setting the transactional propagation to required, and remove all flush():-

@Repository("iUserDao")
@Transactional(propagation=Propagation.REQUIRED)
public class UserDao implements IUserDao {
   ...
}
like image 20
limc Avatar answered Sep 21 '22 01:09

limc