I am new to hibernate and I am studying many-to-one mapping and outer-join. I came up with following exception when I tried to retrieve a records:
Code:
net.sf.hibernate.UnresolvableObjectException: No row with the given identifier exists: 3, of class: example.Team
at net.sf.hibernate.UnresolvableObjectException.throwIfNull(UnresolvableObjectException.java:38)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1918)
at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:68)
at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:215).........
The mapping is as follow:
Code:
<class name="example.Player" table="players">
<id name="id" column="player_id" type="long" unsaved-value="null">
<generator class="hilo"/>
</id>
<property name="firstName" column="first_name" type="string" length="12" not-null="true"/>
<property name="lastName" column="last_name" type="string" length="15" not-null="true"/>
<property name="draftDate" column="draft_date" type="date"/>
<property name="annualSalary" column="salary" type="float"/>
<property name="jerseyNumber" column="jersey_number" type="integer" length="2" not-null="true"/>
<many-to-one name="team" class="example.Team" column="team_id" outer-join="true"/>
</class>
<class name="example.Team" table="teams">
<id name="id" column="team_id" type="long" unsaved-value="null">
<generator class="hilo"/>
</id>
<property name="name" column="team_name" type="string" length="15" not-null="true"/>
<property name="city" column="city" type="string" length="15" not-null="true"/>
<set name="players" cascade="all" inverse="true" lazy="true">
<key column="team_id"/>
<one-to-many class="example.Player"/>
</set>
</class>
The code is as:
Code:
Configuration cfg = new Configuration()
.addClass(example.Player.class)
.addClass(example.Team.class);
SessionFactory factory = cfg.buildSessionFactory();
// open a session and save the team
SessionFactory sessions = cfg.buildSessionFactory();
Session session = sessions.openSession();
// method 1: loading a persistent instance
Iterator it = session.find("from Player").iterator();
while(it.hasNext()) {
Player player = (Player)it.next
System.out.print(player.getId() + ": "+player.getFirstName());
if(player.getTeam() != null)
System.out.print("\t" + player.getTeam().getId());
System.out.println();
}
System.out.println("End:");
Here is the data:
Code:
PLAYER_ID FIRST_NAME LAST_NAME DRAFT_DATE SALARY JERSEY_NUMBER TEAM_ID
--------- ---------- --------- ---------- -------- ------------- -------
300 Ed Todd (null) 12000.0 2 3
I know that there is no team_id 3 in table Teams. With outer-join="true", I am expecting the player.getTeam() is null and the code still works.
Please help me understand where I am wrong and how to correct it.