JPA、Spring 和 ObjectDB 未更新

发布于 2024-12-09 08:30:23 字数 2957 浏览 0 评论 0原文

我是 Spring JPA 与 ObjectDB 数据库配合的新手,但我遇到了一个无法解决的问题。

我有一个使用上述技术编写的应用程序,它工作正常,它保留了新实体等(因此我认为配置 bean 没有问题),除了更新最简单的 OneToMany/<代码>ManyToOne 关系。这些更新不会持久保存到数据库中,我不明白为什么。这是我的代码片段:

Entity Team (1:N):

@Entity
public class Team implements Serializable {
  ...
  List<Player> squad;
  ...
  @OneToMany(mappedBy="team", cascade=CascadeType.PERSIST)
  public List<Player> getSquad() {
    return squad;
  }
  ...
}

Entity Player (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 页面或尝试以相同的方式添加另一个球员,球队始终为空 - 数据库中没有任何内容。既不是球队对象,也不是球员对象。我做错了什么?

任何帮助将不胜感激。谢谢。

编辑:这是我的 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>

PS 绝对路径"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" />

I am quite a newbie to Spring JPA in cooperation with ObjectDB database, but I have encountered a problem that I cannot work out.

I have an application written with the mentioned technologies and it works OK, it persists new entities etc. (thus I think there is no problem with the configuration beans), except for updating even the simplest OneToMany/ManyToOne relations. Those updates are not persisted to the database and I cannot figure out why. Here's the snippet of my code:

Entity Team (1:N):

@Entity
public class Team implements Serializable {
  ...
  List<Player> squad;
  ...
  @OneToMany(mappedBy="team", cascade=CascadeType.PERSIST)
  public List<Player> getSquad() {
    return squad;
  }
  ...
}

Entity Player (N:1)

@Entity
public class Player implements Serializable {
  ...
  private Team team;
  ...
  @ManyToOne
  public Team getTeam() {
    return team;
  }
  ...
}

Here is a snippet from controller using both DAO objects and the problem:

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

When I try to list all players in the team inside the index.jsp page or try to add another player the same way, the squad is always empty - nothing persisted to the database. Neither the team object, nor the player object. What do I do wrong?

Any help would be appreciated. Thanks.

EDIT: here is my 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>

P.S. The absolute path "C:/file.odb" is only for demonstration purposes.

and here is Spring configuration:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十级心震 2024-12-16 08:30:23

CascadeType.PERSIST 级联新对象的持久性,但您从数据库加载玩家并将该玩家附加到您的团队。如果你想级联,你必须添加CascadeType.MERGE

cascade = {CascadeType.PERSIST, CascadeType.MERGE}

你可以看看CascadeType.ALL

CascadeType.PERSIST cascades the persistens of a new object but you load a player from the database and attach the player to your team. If you want to cascade that you have to add the CascadeType.MERGE

cascade = {CascadeType.PERSIST, CascadeType.MERGE}

You may have a look at CascadeType.ALL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文