This is the problem. I have two entities: Profile and User.
1 user has many profiles
But 1 user also has an "active profile". An active profile is the profile this user is currently using.
Table USER:
USER_ID: VARCHAR(10)
NAME: VARCHAR(20)
EMAIL: VARCHAR(20)
SERIAL_NUMBER:VARCHAR(20)
PROFILE_ID: INTEGER (this is the foreign key that references the active profile)
Table PROFILE:
PROFILE_ID: INTEGER
NAME: VARCHAR_20
USER_ID: VARCHAR(10) (This is the Foreign Key that indicutes whose profile is this).
My Classes:
public class User {
private String userID;
private String name;
private String email;
private String serialNumber;
private List profiles;
private Profile activeProfile;
//getters, setters, etc...
}
public class Profile {
private int profileID;
private String name;
//getters, setters, etc...
}
The problem is: After I mapped the activeProfile in the user.hbm.xml I get NullPointerExceptions when I try to retrieve Users that actually have an active profile setted. This error does not occur when I try to retrieve users that do not have active profiles set (Their PROFILE_ID column is null).
Can anyone here please help me?
Here are my .hbm.xml files:
<hibernate-mapping>
<class name=User"
table="USER" >
<id name="userID" column="USER_ID"/>
<property name="name" column="NAME" />
<property name="email" column="EMAIL" />
<property name="serialNumber" column="SERIAL_NUMBER" />
<!--<many-to-one name="activeProfile" column="ACTIVE_PROFILE"
class="Profile"
cascade="all"/>-->
<many-to-one name="activeProfile"
class="com.ibm.extremeblue.callhandling.entities.Profile"
column="PROFILE_ID" insert="false" update="true"/>
<bag name="profiles" table="PROFILE" cascade="all">
<key column="USER_ID" />
<one-to-many class="Profile"/>
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Profile" table="PROFILE">
<id name="profileID" column="PROFILE_ID">
<generator class="identity" />
</id>
<property name="name" column="NAME" />
</class>
</hibernate-mapping>
The exception stack trace is:
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3121)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:830)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
at org.hibernate.type.EntityType.resolve(EntityType.java:303)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:47)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:41)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2730)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:177)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:799)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:792)
at com.ibm.extremeblue.callhandling.persistence.UserDAO.loadUser(UserDAO.java:19)
at com.ibm.extremeblue.callhandling.persistence.UserDAO.main(UserDAO.java:41)
Hibernate version:
3.2
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:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html