Ok this is a weird problem because I had it working before.
So I have 2 classes: game and developer. They have a many to many relationship.
The persistency unit makes automatically the table game_developer.
Thus there are 3 tables: game developer and game_developer.
When I enter Information inside the database. The table game and developer will get there values as normal but the table game_developer will remain empty. So the relationship isn’t recognized.
Also when I run the webapp everything inside the tables should be dropped. The developer drops fine but the game remains to exist.
Any push in the right direction is appreciated.
Thank you in advance,
David
Initialization:
try {
gameOrganizer = (GameOrganizer) getServletContext().getAttribute("database");
Game game = new Game("Counter Strike: source");
Game game2 = new Game("Battlefield: bad company 3");
Game game3 = new Game("Killing floor");
Developer devel = new Developer("valve");
Developer devel2 = new Developer("EA Games");
Developer devel2b = new Developer("DICE");
Developer devel3 = new Developer("Ubisoft");
//The GameOrganizer is the controller between the model en the view.
//The view being the website.
//So gameOrganizer.addGame(game) will persist the object to the database.
gameOrganizer.addGame(game);
gameOrganizer.addGame(game2);
gameOrganizer.addGame(game3);
gameOrganizer.addDeveloper(devel);
gameOrganizer.addDeveloper(devel2);
gameOrganizer.addDeveloper(devel2b);
gameOrganizer.addDeveloper(devel3);
game.addDeveloper(devel);
devel.getGames().add(game);
game2.addDeveloper(devel2);
devel2.getGames().add(game2);
game2.addDeveloper(devel2b);
devel2b.getGames().add(game2);
game3.addDeveloper(devel3);
devel3.getGames().add(game3);
} catch (DatabaseException ex) {
Logger.getLogger(GameController.class.getName()).log(Level.SEVERE, null, ex);
} catch (DomainException ex) {
Logger.getLogger(GameController.class.getName()).log(Level.SEVERE, null, ex);
}
The game class:
@Entity
public class Game implements Serializable{
private String gameNaam;
private double prijs;
@ManyToMany(mappedBy = "games")
private Collection<Developer> developers = new ArrayList<Developer>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Game(){};
.
.
//methods
.
.
}
The Developer class
@Entity
public class Developer implements Serializable {
private String naam;
private String info;
@ManyToMany
private Collection<Game> games = new ArrayList<Game>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Developer() {
}
.
.
//methods
.
.
}
The persistency Unit:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="GameDatabaseSitePU" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<class>domainmodel.Developer</class>
<class>domainmodel.Game</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.jdbc.user" value="app"/>
<property name="toplink.jdbc.password" value="app"/>
<property name="toplink.jdbc.url" value="jdbc:derby://localhost:1527/GameDatabase;create=true"/>
<property name="toplink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
The relationship isn’t recognized because you persist the objects before you establish the relationships. Do the calls
gameOrganizer.add..()after you add the objects to each others collections. Also add persist on cascade to your many-to-many relationship of developers:Then you only need to persist the games.