Hi all :)
I'm having a problem with creating a new object that's defined in an inheritance hierarchy. The basic issue is that when the insert statement is generated for the base class, it's missing two properties that are foreign keys into other tables, causing a constraint violation. The insert statement for the derived class is not generated -- I'm not seeing it on the console, at the very least.
In any case, my object relationships are as follows:
1) A base class, TriviaGame, mapped into users_trivia_games. It references foreign keys in two other tables via the user and triviaGameType properties (also entities);
2) A derived class, SinglePlayerGame, mapped into users_single_player_trivia_games;
3) A User class, mapped into users -- a User has a collection of TriviaGames, and each TriviaGame knows who its User is;
4) A TriviaGameType class, mapped into trivia_game_types -- a TriviaGameType is assigned to each TriviaGame, and a TriviaGameType has a collection of games of its type;
The session factory initializes itself properly, and all other entities in my app work completely. An abridged (extraneous properties removed) version of the TriviaGame hierarchy is below.
Code:
<hibernate-mapping>
<class name="TriviaGame" table="users_trivia_games" polymorphism="implicit">
<id name="userTriviaGameId" type="int">
<column name="user_trivia_game_id" />
<generator class="native" />
</id>
<many-to-one name="user" column="user_id" not-null="true" class="User" insert="false" update="false"/>
<many-to-one name="triviaGameType" column="trivia_game_type_id" not-null="true" class="TriviaGameType" insert="false" update="false"/>
<property name="highestScore" type="int" column="highest_score" not-null="true"/>
<property name="totalQuestions" type="int" column="total_questions" not-null="true"/>
<property name="datePlayed" type="timestamp" column="date_played" not-null="true"/>
<joined-subclass name="SinglePlayerGame" table="users_single_player_trivia_games">
<key column="user_trivia_game_id"/>
<property name="timeElapsed" type="int" column="time_elapsed"/>
</joined-subclass>
</class>
</hibernate-mapping>
Similarly, the mapping for User and TriviaGameType follow:
Code:
<hibernate-mapping>
<class name="User" table="users">
<id name="userId" type="int">
<column name="user_id" />
<generator class="native" />
</id>
<set name="triviaGames" inverse="true" table="users_trivia_games">
<key column="user_id" not-null="true" />
<one-to-many class="TriviaGame" />
</set>
</hibernate-mapping>
<hibernate-mapping>
<class name="TriviaGameType" table="trivia_game_type">
<id name="triviaGameTypeId" type="int">
<column name="trivia_game_type_id" />
<generator class="native" />
</id>
<set name="triviaGames" table="users_trivia_games">
<key column="trivia_game_type_id" />
<one-to-many class="TriviaGame" />
</set>
</class>
</hibernate-mapping>
Everything goes fine until I try to save the new game:
Code:
Session hibernateSession = HibernateSessionFactory.getSession();
Transaction tx = hibernateSession.beginTransaction();
User user = (User)hibernateSession.getNamedQuery("userById").setInteger("userId", createGame.getPlayer().getId()).uniqueResult();
TriviaGameType triviaGameType = (TriviaGameType)hibernateSession.getNamedQuery("singlePlayerGameType").uniqueResult();
Set<TriviaGame> userTriviaGames = user.getTriviaGames();
Set<TriviaGame> triviaGameTypeTriviaGames = triviaGameType.getTriviaGames();
// Create a new TriviaGame entry for the User
SinglePlayerGame game = new SinglePlayerGame(user, triviaGameType);
game.setDatePlayed(new Date());
userTriviaGames.add(game);
triviaGameTypeTriviaGames.add(game);
hibernateSession.save(game);
I see that the following insert statement is created, but nothing more:
Code:
Hibernate: insert into users_trivia_games (highest_score, total_questions, date_played) values (?, ?, ?)
I'm expecting to see "user_id" and "trivia_game_type_id" in the column list as well, as well as an insert into the users_single_player_trivia_games table. The foreign key constraint failure comes, again, from user_id not being provided.
I realize this is a lot of stuff, but I'm really stumped. I'd like to avoid removing the inheritance, and I'm not sure what I'm doing wrong as I have MANY other types mapped and working this exact same way.
Thanks in advance for any help.
C