Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.1 beta1 and anootations Beta4
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
I am running MySQL 4.1.11 with a JDBC driver 3.1.10 and I was getting strange NullPointerExceptions when creating an Organization and reading it back out. Let's start with the entity definitions.
The Organization (and its parent Party) classes are defined like this:
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name = "Org")
public class Organization extends Party
{
@Transient
public Long getOrgId()
{
return getPartyId();
}
public void setOrgId(final Long orgId)
{
setPartyId(orgId);
}
}
@Entity()
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name = "Party")
public class Party
{
private Long partyId;
@Id(generate=GeneratorType.AUTO)
@Column(name = PK_COLUMN_NAME)
public Long getPartyId()
{
return partyId;
}
protected void setPartyId(final Long partyId)
{
this.partyId = partyId;
}
}
So it's a pretty straight forward relationship and the test code is like this:
Code:
Session session = openSession();
Transaction transaction = session.beginTransaction();
final Organization blueCross = new Organization();
blueCross.setOrganizationName("Blue Cross Blue Shield");
session.save(blueCross);
transaction.commit();
session.close();
session = openSession();
final Organization blueCross2 = (Organization) session.createCriteria(Organization.class).add(Restrictions.eq("partyId", blueCross.getOrgId())).uniqueResult();
assertThat(blueCross2.getOrganizationName(), eq(blueCross.getOrganizationName()));
So the NPE is being thrown at the
assertThat line on the
blueCross2.getOrganizationName(). Something simple like this and it was not retrieving the object back. If I don't use the
.add(Restrictions.eq("partyId", blueCross.getOrgId())) part in the criteria then a valid Organization comes back and the test passes (there are no other orgs in the database). This really had me baffled until I saw this post:
http://forum.hibernate.org/viewtopic.php?t=945424
So I rolled back to mysql driver version 3.0.17 and IT WORKED but this makes no sense at all. Has anyone seen this behavior at all? It would be nice to find out what the actual cause was because it's killing me. :)