Hibernate mapping and query
The hibernate mapping of my hbm.xml is:
<class name="UserCalendar" table="user_calendar">
<id name="userCalendarId" column="user_calendar_id" type="long">
<generator class="native" />
</id>
<property name="userId" column="user_id" type="long" not-null="true"/>
<property name="userLoginName" column="user_login_name" type="string" not-null="true" length="32000" />
<property name="userName" column="user_name" type="string" not-null="true" length="32000" />
<list name="userDates" cascade="all" lazy="false">
<key column="user_calendar_id"/>
<index column="idx"/>
<one-to-many class="UserDate"/>
</list>
</class>
<class name="UserDate" table="user_date">
<id name="userDateId" column="user_date_id" type="long">
<generator class="native" />
</id>
<property name="date" column="date" type="date"/>
<list name="userItems" cascade="all" lazy="false">
<key column="user_date_id"/>
<index column="idx"/>
<one-to-many class="UserItem"/>
</list>
</class>
<class name="UserItem" table="user_item">
<id name="userItemId" column="user_item_id" type="long">
<generator class="native"/>
</id>
<property name="spaceId" column="space_id" type="long"/>
<property name="spaceName" column="space_name" type="string"/>
<property name="itemRefId" column="item_ref_id" type="string"/>
<property name="itemId" column="item_id" type="long"/>
<property name="allocation" column="allocation" type="double"/>
<property name="scheduledStrategy" column="scheduled_strategy" type="integer"/>
<property name="utilization" column="utilization" type="double"/>
<property name="deadline" column="deadline" type="date"/>
<property name="ticketType" column="ticket_type" type="integer"/>
<property name="isCurrentlyAssigned" column="is_currently_assigned" type="boolean"/>
</class>
The UserCalendar.java:
public class UserCalendar implements Serializable {
private static final long serialVersionUID = 10172L;
private long userCalendarId;
private long userId;
private String userLoginName;
private String userName;
private List<UserDate> userDates;
public UserCalendar() {
}
public long getUserCalendarId() {
return userCalendarId;
}
public void setUserCalendarId(long userCalendarId) {
this.userCalendarId = userCalendarId;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getUserLoginName() {
return userLoginName;
}
public void setUserLoginName(String userLoginName) {
this.userLoginName = userLoginName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public List<UserDate> getUserDates() {
return userDates;
}
public void setUserDates(List<UserDate> userDates) {
this.userDates = userDates;
}
}
The UserDate.java:
public class UserDate implements Serializable {
private static final long serialVersionUID = 10173L;
private long userDateId;
private Date date;
private List<UserItem> userItems;
public UserDate() {
}
public long getUserDateId() {
return userDateId;
}
public void setUserDateId(long userDateId) {
this.userDateId = userDateId;
}
public void setDate(Date date) {
this.date = date;
}
public Date getDate() {
return date;
}
public List<UserItem> getUserItems() {
return userItems;
}
public void setUserItems(List<UserItem> userItems) {
this.userItems = userItems;
}
}
And the UserItem.java:
public class UserItem implements Serializable {
private static final long serialVersionUID = 10174L;
public static final int AUTO = 1;
public static final int EQUAL = 2;
public static final int CUSTOM = 3;
public static final int CUSTOM_EQUAL = 4;
public static final int GENERAL_TICKET = 5;
public static final int SPECIAL_TICKET_HALF = 6;
public static final int SPECIAL_TICKET_FULL = 7;
private long userItemId;
private long spaceId;
private String spaceName;
private long itemId;
private String itemRefId;
private double allocation;
private int scheduledStrategy;
private double utilization;
private Date deadline;
private int ticketType;
private boolean isCurrentlyAssigned;
public UserItem() {
}
public long getUserItemId() {
return userItemId;
}
public void setUserItemId(long userItemId) {
this.userItemId = userItemId;
}
public long getSpaceId() {
return spaceId;
}
public void setSpaceId(long spaceId) {
this.spaceId = spaceId;
}
public long getItemId() {
return itemId;
}
public void setItemId(long itemId) {
this.itemId = itemId;
}
public double getAllocation() {
return allocation;
}
public void setAllocation(double allocation) {
this.allocation = allocation;
}
public int getScheduledStrategy() {
return scheduledStrategy;
}
public void setScheduledStrategy(int scheduledStrategy) {
this.scheduledStrategy = scheduledStrategy;
}
public double getUtilization() {
return utilization;
}
public void setUtilization(double utilization) {
this.utilization 开发者_如何学C= utilization;
}
public Date getDeadline() {
return deadline;
}
public void setDeadline(Date deadline) {
this.deadline = deadline;
}
public int getTicketType() {
return ticketType;
}
public void setTicketType(int ticketType) {
this.ticketType = ticketType;
}
public String getSpaceName() {
return spaceName;
}
public void setSpaceName(String spaceName) {
this.spaceName = spaceName;
}
public String getItemRefId() {
return itemRefId;
}
public void setItemRefId(String itemRefId) {
this.itemRefId = itemRefId;
}
public boolean isCurrentlyAssigned() {
return isCurrentlyAssigned;
}
public void setCurrentlyAssigned(boolean isCurrentlyAssigned) {
this.isCurrentlyAssigned = isCurrentlyAssigned;
}
}
Now I want to select those UserCalendar
object which has a specific itemId
. In my thought if I insert userCalendarId
in UserItem
and execute
SELECT USERCALENDAR
FROM USER_CALENDAR
WHERE USERCALENDAR.USERCALENDARID IN
(SELECT USERCALENDARID
FROM USERITEM
WHERE USERITEM.ITEMID=ID)
then it might be possible.
Am I right?
How can I insert userCalendarId
in UserItem
? What is the mapping needed for this?
Thanks and regards.
To have one-to-many
you will need column in UserDate
that refers to UserCalendar.userCalendarId
and column in UserItem
to refer to UserDate.userDateId
. You will need to specify those columns via many-to-one
in those classes.
Then you can use join
SELECT * FROM user_calendar uc
JOIN user_date ud ON ud.user_calendar_id=uc.user_calendar_id
JOIN user_item ui ON ui.user_date_id=ud.user_date_id
WHERE ui.user_item_id=<your value>
Assuming that you have those new columns user_calendar_id
and user_date_id
in user_date
and user_item
.
In HQL it will be simpler but I don't remember on top of my head. Check docs. It could be something like
select ui.userDate.userCaledar from UserItem ui where ui.userItemId=:id
Provided that you added properties userDate
and userCalendar
via many-to-one
精彩评论