简体   繁体   中英

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).
Then at the Server, hibernate save Comments with id=0 into database, and give a generated id.

How can I assign these IDs to Comments in the Client? If not, the other time I call update(Record reocord), Comments will be add twice.


Maybe: Record update(Record reocord), but I think if Record have large data it's not wise.

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. Do remember to override hashcode and equals for comparison purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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