开发者

JPA, Spring and ObjectDB not updating

开发者 https://www.devze.com 2023-04-12 12:49 出处:网络
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 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 -> wo开发者_如何学Pythonrking 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" />


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号