简体   繁体   English

RMI +休眠,客户端如何获取生成的ID

[英]RMI+hibernate,how can client get generated id

@Entity
public class Record implements Serializable{

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;

@OneToMany(fetch=FetchType.EAGER)
@Fetch(FetchMode.SUBSELECT)
@Cascade(value={CascadeType.SAVE_UPDATE})
private List<Comment> commentList=new ArrayList<Comment>();

}

@Entity
public class Comment implements Serializable{

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;

@Column(columnDefinition="text")
private String content;
}

I have a Record in my Client, and add SOME comments in the list. 我的客户中有一条记录,并在列表中添加了一些注释。
Use the RMI method: void update(Record reocord). 使用RMI方法:void update(Record reocord)。
Then at the Server, hibernate save Comments with id=0 into database, and give a generated id. 然后在服务器上,休眠将id = 0的注释保存到数据库中,并给出一个生成的id。

How can I assign these IDs to Comments in the Client? 如何将这些ID分配给客户端中的注释? If not, the other time I call update(Record reocord), Comments will be add twice. 如果没有,我下次调用update(Record reocord)时,注释将被添加两次。


Maybe: Record update(Record reocord), but I think if Record have large data it's not wise. 也许:Record update(Record reocord),但是我认为,如果Record有大量数据,那是不明智的。

I faced this issue. 我遇到了这个问题。 If you are autogenerating the primary key (which can be mostly used for normalizing tables), Create a unique Id yourself based on the existing data. 如果要自动生成主键(主要用于标准化表),请根据现有数据自己创建唯一ID。 Do remember to override hashcode and equals for comparison purposes. 切记重写hashcodeequals以进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM