我对Spring JPA和ObjectDB数据库的合作是一个新手,但是我遇到了一个我无法解决的问题。JPA,Spring和ObjectDB没有更新
我有一个应用程序编写的上述技术,它的工作正常,它坚持新的实体等(因此我认为没有问题的配置豆),除了更新甚至最简单的关系OneToMany
/ManyToOne
关系。这些更新不会持久到数据库,我不明白为什么。下面是我的代码片段:
实体队(1:N):
@Entity
public class Team implements Serializable {
...
List<Player> squad;
...
@OneToMany(mappedBy="team", cascade=CascadeType.PERSIST)
public List<Player> getSquad() {
return squad;
}
...
}
实体播放器(N:1)
@Entity
public class Player implements Serializable {
...
private Team team;
...
@ManyToOne
public Team getTeam() {
return team;
}
...
}
这里是同时使用DAO对象从控制器片段而问题:
public ModelAndView addPlayer(HttpServletRequest request, HttpServletResponse response) throws Exception {
...
Team t = teamDao.getTeamById(1); // retrieves an object with ID=1
Player p = playerDao.getPlayerById(1); // retrieves a player with ID=1
t.getSquad().add(p); // adds a player to the squad -> working fine but does not persist
System.out.println("Size of squad: " + t.getSquad().size()); // the player is there
...
return new ModelAndView("index.jsp", "team", t);
}
当我尝试列出index.jsp
页面或TR里在球队所有球员y以相同的方式添加其他玩家,小队总是空的 - 没有任何东西持续存在于数据库中。团队对象,也不是玩家对象。我做错了什么?
任何帮助,将不胜感激。谢谢。
编辑:这里是我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="NewPU" transaction-type="RESOURCE_LOCAL">
<provider>com.objectdb.jpa.Provider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="C:/file.odb" />
<property name="javax.persistence.jdbc.user" value="admin"/>
<property name="javax.persistence.jdbc.password" value="admin"/>
</properties>
</persistence-unit>
附:绝对路径"C:/file.odb"
仅用于演示目的。
这里是Spring配置:一个新的对象
<mvc:annotation-driven />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="NewPU" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
没有彻底地读你的代码,这可能是原因:'cascade = CascadeType.PERSIST' - 你只是级联持久。 – Thomas