映射与Composite-id的关联

时间:2013-03-04 09:09:35

标签: hibernate associations composite-id

我是Hibernate的新手,我对如何表示这种情况表示怀疑:

- >表格消息: - (PK)MsgSender - >表'用户'的外键 - (PK)MsgDestination - >表'用户'的外键 - (PK)MsgDateSend - MsgContent - IsReaded

- >表用户 - (PK)用户名 - ......

这是我的Message.hbm:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="Message" table="MESSAGE">
        <composite-id name="id" class="MessageId">
            <key-many-to-one name="MsgSender" class="User">
                <column name="msgSender"/>
            </key-many-to-one>
            <key-many-to-one name="MsgDestination" class="User">
                <column name="msgDestination"/>
            </key-many-to-one>
            <key-property name="msgDateSend" type="double" column="MsgDateSend"/>
        </composite-id>
        <property name="msgContent" type="java.lang.String">
            <column name="MsgContent" />
        </property>
        <property name="isReaded" type="boolean" access="field">
            <column name="ISREADED" />
    </property>
    </class>
</hibernate-mapping>

这是我的POJO: 课程信息:

public class Message implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = -5319468638732762052L;
private MessageId id;
private String msgContent;
private boolean isReaded;

public Message() {
    this.id = new msgContentId();
}

/**
 * @return the id
 */
public MessageId getId() {
    return id;
}

/**
 * @param id the id to set
 */
public void setMessageId(MessageId mId) {
    this.id = mId;
}

/**
 * @return the msgContent
 */
public String getMsgContent() {
    return msgContent;
}

/**
 * @param msgContent
 *            the msgContent to set
 */
public void setMsgContent(String msgContent) {
    this.msgContent = msgContent;
}

/**
 * @return the isReaded
 */
public boolean getIsReaded() {
    return isReaded;
}

/**
 * @param isReaded
 *            the isReaded to set
 */
public void setIsReaded(boolean isReaded) {
    this.isReaded = isReaded;
}
}

Class MessageId:

public class MessageId implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 1L;
User msgSender;
User msgDestination;
double msgSend;


/**
 * @return the msgSender
 */
public User getMsgSender() {
    return msgSender;
}

/**
 * @param msgSender
 *            the msgSender to set
 */
public void setMsgSender(User msgSender) {
    this.msgSender = msgSender;
}

/**
 * @return the msgDestination
 */
public User getMsgDestination() {
    return msgDestination;
}

/**
 * @param msgDestination
 *            the msgDestination to set
 */
public void setMsgDestination(User msgDestination) {
    this.msgDestination = msgDestination;
}

/**
 * @return the msgSend
 */
public double getMsgSend() {
    return msgSend;
}

/**
 * @param msgSend
 *            the msgSend to set
 */
public void setMsgSend(double msgSend) {
    this.msgSend = msgSend;
}
}

使用这种结构,Hibernate不会插入,删除,... 有谁能帮我解决这个问题?

提前致谢。

0 个答案:

没有答案