1

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

类消息 ID:

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 不会进行插入、删除……有没有人可以帮我解决这个问题?

提前致谢。

4

0 回答 0