Hibernate映射和查询

时间:2011-06-07 13:10:30

标签: java hibernate mapping

我的hbm.xml的hibernate映射是:

<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>

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;
    }

}

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;
    }
}

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 = 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;
    }   
}

现在我想选择那些具有特定UserCalendar的{​​{1}}对象。在我的想法中,如果我在itemId中插入userCalendarId并执行

UserItem
那么它可能是可能的。

我是对的吗?

如何在SELECT USERCALENDAR FROM USER_CALENDAR WHERE USERCALENDAR.USERCALENDARID IN (SELECT USERCALENDARID FROM USERITEM WHERE USERITEM.ITEMID=ID) 中插入userCalendarId?这需要什么映射?

谢谢和问候。

1 个答案:

答案 0 :(得分:0)

one-to-many UserDate,您需要UserCalendar.userCalendarId中引用UserItem的列和UserDate.userDateId中的列以引用many-to-one。您需要在这些类中通过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> 指定这些列。

然后你可以使用join

user_calendar_id

假设您在user_date_iduser_date中有新列user_itemselect ui.userDate.userCaledar from UserItem ui where ui.userItemId=:id

在HQL中它会更简单,但我不记得了。检查文档。 它可能像

userDate

如果您通过userCalendar

添加了属性many-to-one和{{1}}