1

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" />
ObjectDB
  • 1,312
  • 8
  • 9
  • Without thoroughly reading your code, this might be the cause: `cascade=CascadeType.PERSIST` - you're only cascading persists. – Thomas Oct 13 '11 at 07:49

1 Answers1

1

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.

Peter
  • 4,752
  • 2
  • 20
  • 32
  • You are right, I should have used CascadeType.ALL. I have just changed that, but nothing happened. In addition, I have found out that there is some more global problem, because NO CHANGES at all can be done to the persisted entities. Even the simplest t.setName("new name") does not work. It changes the name within the object on the page, but does not persist it to the database. I know it is difficult to give advice without having the entire code to look at, but if anybody has a clue what might cause that, I can provide any information, any code, I just need to sort it out. Thank you – Pietro Falconi Oct 13 '11 at 08:07
  • I've appended it to the question above. Thanks – Pietro Falconi Oct 13 '11 at 08:28
  • Try to annotate your `addPlayer` controller action with `@Transactional`. – Peter Oct 13 '11 at 08:29
  • YES!!! That did the trick. I thought it would be something as simple as that :-). I had the @Transactional annotation on the dao's persist() method, even tried to put it on the whole dao class, but didn't put it to the method in the controller... Thank you very much, you saved hours of my time. – Pietro Falconi Oct 13 '11 at 08:36