1

我有这个我无法解决的问题,这是我的实体类:

/** * @hibernate.class * table="users.network_topic" * @hibernate.cache usage="read-write" */ 公共类 NetworkTopic 实现 Serializable, Idable{

/** identifier field */
private Long id;

/** persistent field */
private Long networkId;

/** persistent field */
private Long topicId;

private UserTopic topic;
private Network network;

/** 
 *            @hibernate.id
 *             generator-class="assigned"
 *             type="java.lang.Long"
 *             column="id"
 */
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

/** 
 *            @hibernate.property
 *             type="java.lang.Long"
 *             column="network_id"
 *             not-null="true"
 */
public Long getNetworkId() {
    return networkId;
}

public void setNetworkId(Long networkId) {
    this.networkId = networkId;
}

/** 
 *            @hibernate.property
 *             type="java.lang.Long"
 *             column="user_topic_id"
 *             not-null="true"
 */
public Long getTopicId() {
    return topicId;
}

public void setTopicId(Long topicId) {
    this.topicId = topicId;
}   

/** 
 *            @hibernate.set
 *             lazy="true"
 *             inverse="true"
 *             cascade="none"
 *            @hibernate.key
 *             column="user_topic_id"
 *            @hibernate.one-to-many
 *             class="com.netblue.matchpoint.domain.UserTopic"
 *         
 */
public UserTopic getTopic() {
    return topic;
}

public void setTopic(UserTopic topic) {
    this.topic = topic;
}   

/** 
 *            @hibernate.set
 *             lazy="true"
 *             inverse="true"
 *             cascade="none"
 *            @hibernate.key
 *             column="network_id"
 *            @hibernate.one-to-many
 *             class="com.netblue.matchpoint.domain.Network"
 *         
 */    
public Network getNetwork() {
    return network;
}

public void setNetwork(Network network) {
    this.network = network;
}   

@Override
public String toString() {
    return "Network[id=" + id + "]";
}

}

然后我尝试用这个代码保存一个新的寄存器:

        UserTopic topic = new UserTopic();
        topic.setId(topicId);
        topic.setName(ParseUtil.getString(map.get(MPConstants.TOPIC_NAME_PARAM)));
        topic.setCreatedBy(ParseUtil.getInt(map.get(MPConstants.USER_ID)));
        Date now = new Date();
        topic.setCreatedDt(now);
        topic.setLastUpdatedDt(now);
        topic.setStatusId(Status.ACTIVE);
        topic.setActivityCnt(0);
        userTopicDao.saveOrUpdate(topic);

        UserTopicMap utm = new UserTopicMap();
        utm.setId(topicId);
        utm.setMtid(topicId);
        utm.setOtid(topicId);
        utm.setIsDeleted(false);
        utm.setLastUpdatedDt(now);
        userTopicMapDao.saveOrUpdate(utm);

        Network network = MpAuctionUtil.getNetworkById(networkId);
        NetworkTopic networkTopic = new NetworkTopic();
        networkTopic.setNetworkId(networkId);
        networkTopic.setTopicId(topicId);
        networkTopic.setNetwork(network);
        networkTopic.setTopic(topic);
        LOG.debug("userTopicService addObjSubscriber x networkId="+networkId+", topicId="+topicId);
        networkTopicDao.saveOrUpdate(networkTopic);

最后几行是失败的,如果我删除它们它可以工作,我的意思是它可以保存主题,但是当我尝试这样做时它失败了,我不知道我是否做错了什么,请帮助我! !

4

2 回答 2

0

NetworkTopic 类尚未分配 id。您必须使用 setId 方法或考虑考虑其他 id 策略之一

2.2.3.1。生成标识符属性

于 2012-09-06T17:49:43.073 回答
0
NetworkTopic networkTopic = new NetworkTopic();
    networkTopic.setNetworkId(networkId);
    networkTopic.setTopicId(topicId);
    networkTopic.setNetwork(network);
    networkTopic.setTopic(topic);
    LOG.debug("userTopicService addObjSubscriber x networkId="+networkId+", topicId="+topicId);
    networkTopicDao.saveOrUpdate(networkTopic);

在这里,您必须调用该方法setId,并手动设置值,因为您的 id 生成器是“分配的”,这意味着您必须在每次插入或更新数据之前手动分配!

于 2012-09-08T01:08:19.467 回答