Hi. I'm new to hibernate..
I need a little help about an association mapping.
Take a simple scenario:
user <many-to-one> level
ID......................................ID
Name.................................Name
LevelID
I need to get level name of a user, but I'm not sure what is the best way.
Currently what I am using:
User POJO / Mapping
Code:
private Integer id;
private String name;
private Integer levelid;
private Level level; // extra class to get level name
Code:
<class name="User" table="User" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="Name" />
</property>
<property name="levelId" type="java.lang.Integer">
<column name="LevelID" />
</property>
<many-to-one name="level" class="Level" column="LevelID" insert="false" update="false" unique="false" />
</class>
Level POJO
Code:
private Integer id;
private String name;
Code:
<class name="Level" table="Level" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="Name" />
</property>
</class>
When I got a
user object loaded from DB, I can get it's LevelName by
user.level.getName(), which causes
2 select queries:
Code:
select ... from User ... >> only once
select ... from Level where ID= ... >> per different Level ID
I can do it in pure sql by
one query:
Code:
Select User.ID, User.Name, Level.Name From User, Level Where Level.Id = User.LevelID
Question updated:
pease see http://forum.hibernate.org/viewtopic.ph ... 01#2335001
thanks.