views:

113

answers:

1

Hello last few hours I try to test with spring jpa 2.0 3.0 Finally I could recover objects persisted but when I try to persist a new object I receive the following error message:

org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:306) at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:102) at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) at org.springframework.orm.jpa.JpaAccessor.translateIfNecessary(JpaAccessor.java:152) at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:188) at org.springframework.orm.jpa.JpaTemplate.flush(JpaTemplate.java:288) at myPackage.testDAO.create(testDAO.java:33) at myPackage.Main.main(Main.java:27) Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:789) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365) at $Proxy21.flush(Unknown Source) at org.springframework.orm.jpa.JpaTemplate$8.doInJpa(JpaTemplate.java:290) at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:183) ... 3 more

my entity bean:

@Entity public class Application implements Serializable { private static final long serialVersionUID = 1L;

@Id
@Column(name="ID_APPLICATION")
private long idApplication;

@Temporal( TemporalType.DATE)
@Column(name="DATE_LIVRAISON")
private Date dateLivraison;

@Lob()
private String description;

@Column(name="NOM_APPLICATION")
private String nomApplication;

private String url;

//bi-directional many-to-one association to Test
@OneToMany(mappedBy="application")
private List<Test> tests;

public Application() {
}

public long getIdApplication() {
    return this.idApplication;
}

public void setIdApplication(long idApplication) {
    this.idApplication = idApplication;
}

public Date getDateLivraison() {
    return this.dateLivraison;
}

public void setDateLivraison(Date dateLivraison) {
    this.dateLivraison = dateLivraison;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getNomApplication() {
    return this.nomApplication;
}

public void setNomApplication(String nomApplication) {
    this.nomApplication = nomApplication;
}

public String getUrl() {
    return this.url;
}

public void setUrl(String url) {
    this.url = url;
}

public List<Test> getTests() {
    return this.tests;
}

public void setTests(List<Test> tests) {
    this.tests = tests;
}

}

my repository:

@Repository public class testDAO extends JpaDaoSupport implements ItestDAO { @PersistenceContext private EntityManager em;

public List<Application> findALL() {
    // TODO Auto-generated method stub
    return null;
}

public Application findById(long id) {
    // TODO Auto-generated method stub
    return getJpaTemplate().find(Application.class, id);
}

public void create(Application application) {
    getJpaTemplate().persist(application);
}

}

findById method works normally (which reassures me that jpa configuration is correct), but when I run the create method I receive the above error message.

ApplicationContext context=new ClassPathXmlApplicationContext("application-context.xml");
    testDAO dao=(testDAO)context.getBean("dao");
    Application application=new Application();
    application.setIdApplication(2);
    application.setUrl("url");
    application.setDescription("description");
    application.setNomApplication("dsdsds");
    dao.create(application);

any help are appreciated Thank you for your help

+3  A: 

In short, methods of EntityManager such as persist, remove, merge must be called inside a transaction, hence the error message which is actually self explaining:

javax.persistence.TransactionRequiredException: no transaction is in progress

Spring provides support for transaction management (see links below), including when writing tests... if you use one of the Spring testing class providing transactional support.

See also

Pascal Thivent
Thank you for your reply
@user405458: You're welcome.
Pascal Thivent