tags:

views:

203

answers:

1

Hi every One, I have these class :

@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

As you can see these table have relation with HumnEntity and PictureEntity and ... table. in my domain layer I have Branch class that was dependant on Branch table and all of it's relation. when domain Pass a Branch Object and I want change the Branch in Database I have some Problem that explain below. In Domain Layer in Human class I don't have Identifier(Primary Key) then when I want SaveOrUpdate(branchEntity) I must set all attribute but when I want set branchEntity.setHumanEntity I must query again and get Identifier from database and set to humanEntity then set branchEntity.setHumanEntity. I wanna know that was right? or I do wrong some where ? am I must keep HumanEntity Identifier in My Human class in domain Layer? Indeed I don't know what exactly happen when save or SaveOrUpdate or Update or ... and Object that have RElation with other Object? how I must handle that Object?

A: 

let me said my exact problem when I want change My table branch I use this branchDao.update(be);

but by this code I have error:

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);

when I comment be.setHuman(he); it's update Branch but put null on human_fk!!! when dun comment this error occurred: said another object with this identifier Exist. Am I must First delete all related Object to Branch then set them again?

Am1rr3zA