我有这些课程:
@Entity
@Table(name = "branch", uniqueConstraints = {@UniqueConstraint(columnNames = {"bname", "branch_fk"})})
public class BranchEntity implements Serializable {
@Id
@GeneratedValue
private int id;
@Column(name = "username", length = 64, nullable = false)
private String userName;
@Column(name = "bname", length = 64)
private String branchName;
@Column(name = "studcount")
private int studCount;
@Column(name = "blevel", columnDefinition = "int default 0")
private int level;
@Column(name = "confirmed", columnDefinition = "tinyint default 0")
private int confirmed;
@OneToMany(mappedBy = "branch", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<BranchBuildingEntity> branchbuilding = new HashSet<BranchBuildingEntity>();
@OneToMany(mappedBy = "branch", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<PictureEntity> picture = new HashSet<PictureEntity>();
@OneToOne(mappedBy = "branch", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private LoginEntity login;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "human_fk", nullable = true)
private HumanEntity human;
//some setter and getter
正如您所看到的,这些表与HumnEntity和PictureEntity以及...表有关系。 在我的域层中,我有Branch类,它依赖于Branch表及其所有关系。 当域传递一个分支对象,我想要更改数据库中的分支我有一些问题,解释如下。 在Human类的Domain Layer中我没有Identifier(主键)然后当我想要SaveOrUpdate(branchEntity)时我必须设置所有属性但是当我想设置branchEntity.setHumanEntity时我必须再次查询并从数据库获取Identifier并设置为humanEntity然后设置branchEntity.setHumanEntity。 我想知道那是对的吗?或者我错了一些在哪里?我必须在域层中的Human Human类中保留HumanEntity Identifier吗? 实际上我不知道保存或SaveOrUpdate或更新或者与其他对象具有RElation的对象时到底发生了什么?我该如何处理该对象?
答案 0 :(得分:0)
让我说出我的确切问题
当我想要更改我的表格分支时,我使用此branchDao.update(be);
但是通过这段代码我有错误:
pkId = branch.getId();
uname = branch.getUsername();
password = branch.getPassword();
bname = branch.getName();
studentCount = branch.getStudentNum();
level = branch.getLevel();
otherBuilding = branch.getOtherBuildings();
ofname = branch.getBoss().getFirstName();
olname = branch.getBoss().getLastName();
oemail = branch.getBoss().getEmail();
HumanEntity he = humanDao.getBossByProperties(oemail);
BranchEntity be = new BranchEntity();
be.setHuman(he);
be.setId(pkId);
be.setBranchName(bname);
be.setConfirmed(confirmed);
be.setLevel(level);
be.setStudCount(studentCount);
be.setUserName(uname);
branchDao.update(be);
当我发表评论be.setHuman(he);
时,它更新了分支,但在human_fk上放了空!
当dun评论发生此错误时:
用另一个带有此标识符的对象表示存在。
我必须首先删除所有相关的Object to Branch然后重新设置它们吗?