gpt4 book ai didi

java - 集成 JPA2.0 和 Spring

转载 作者:行者123 更新时间:2023-11-29 06:20:20 25 4
gpt4 key购买 nike

最近几个小时我尝试使用 spring jpa 2.0 3.0 进行测试最后我可以恢复对象持久化但是当我尝试保留一个新对象时,我收到以下错误消息:

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

我的实体 bean:

@实体公共(public)类应用程序实现序列化{ 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;
}

我的仓库:

@存储库公共(public)类 testDAO 扩展 JpaDaoSupport 实现 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 方法正常工作(这让我确信 jpa 配置是正确的),但是当我运行 create 方法时,我收到上述错误消息。

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

感谢任何帮助谢谢你的帮助

最佳答案

简而言之,EntityManager 的方法例如persist , remove , merge必须在事务内部调用,因此错误消息实际上是 self 解释的:

javax.persistence.TransactionRequiredException: no transaction is in progress

Spring 提供对事务管理的支持(参见下面的链接),包括在编写测试时...如果您使用提供 transactional support 的 Spring 测试类之一.

另见

关于java - 集成 JPA2.0 和 Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3388796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com