I have an issue with parent/child updation. I have a class User, which has an One to Many relation with another Class UserExtrnRel. I opulate values for the child in a set, that time the linking field User_id is not generated. while saving the value user_id is generated and updated in the parent table. but user_id is updated as null in the child table
public class User extends DefaultTableMapping implements UserI {
private Integer userId; private String firstName; private String lastName; private String ntLogin; private String email; private Set profiles; private boolean isBOUser;
private Integer boUserId; private String boPassword;
private Boolean active;
private Set boUsers;
@Id @Column(name = "USER_ID") @GeneratedValue(generator="userInsSeq") public Integer getUserId() { return userId; }
public void setUserId(Integer userId) { this.userId = userId; }
//,cascade = CascadeType.ALL .LAZY @JoinColumn(name = "USER_ID" , insertable=false, updatable=false) @OneToMany(cascade = CascadeType.ALL, targetEntity = UserExtrnRel.class, fetch = FetchType.EAGER ) @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN}) public Set getUserExtrnRel() { return boUsers; }
public void setUserExtrnRel(Set boUsers) { this.boUsers = boUsers; } .... }
__________________________________________________________________________________
@Entity @Table(name = "PANDA_USER_EXT_PROG") @SequenceGenerator(name = "userExtRelSeq", sequenceName = "USEREXPROD_SEQ") public class UserExtrnRel implements Serializable {
//private String ntLogin; private Integer userId; private Integer extPrgId; private Integer boUserId; private String boUserPasswd; private Integer userExprodId;
//private User user;
@Id @Column(name = "USEREXPROD_ID") @GeneratedValue(generator="userExtRelSeq") public Integer getExprodId() { return userExprodId; }
public void setExprodId(Integer userExprodId) { this.userExprodId = userExprodId; }
// @JoinColumn(name = "USER_ID") // @ManyToOne(targetEntity = User.class, fetch = FetchType.LAZY) @Column(name = "USER_ID") public Integer getUserId() { return userId; }
public void setUserId(Integer userId) { this.userId = userId; } ...
}
....................................................
public User uploadUser(UserI userI, UserSession userSession) {
User newUser = UserFactory.getUser(userI);
System.out.println("newUser.getExternalProgramId()::"+userI.getExternalProgramId()); User existingUser = null;
try { existingUser = pandaServiceDAO.getUserByPid(newUser.getNtLogin());
if (existingUser == null) {
if (newUser.getIsBOUser()) this.pandaServiceDAO.createBOUser(newUser, userI.getExternalProgramId(),"N"); this.pandaServiceDAO.save(newUser, userSession);
} else if (newUser.getIsBOUser() && (existingUser.getBoUserId() == null || existingUser.getBoUserId().equals(0))) {
this.pandaServiceDAO.createBOUser(existingUser); this.pandaServiceDAO.update(existingUser, userSession);
}
} catch (Exception e) { log.error("Error while saving User '" + newUser.getNtLogin() + "' because: " + e.getMessage(), e); }
return existingUser != null ? existingUser : newUser; }
..................................................
public void createBOUser(User user, Integer BoExpPrg, String existNew) throws PandaServiceDAOHelperException { BOClient boClient = null;
System.out.println(" BoExpPrg :" + BoExpPrg); //passing cms details as list List BoCmsDet = getBODetails(BoExpPrg);
try { boClient = BOClient.getInstance(BoCmsDet);
BOUser boUser = boClient.createUser(user.getNtLogin(), user.getFirstName() + " " + user.getLastName(), user.getEmail()); user.setBoUserId(new Integer(boUser.getBoUserId())); user.setBoPassword(boUser.getBoPassword());
Set<UserExtrnRel> newBoExtRel = new LinkedHashSet<UserExtrnRel>(); /* Set<UserExtrnRel> existBoExtRel = user.getUserExtrnRel();
for (Iterator it=existBoExtRel.iterator(); it.hasNext(); ) { UserExtrnRel o = (UserExtrnRel) it.next(); System.out.println("Exist BO userid :"+o.getBoUserId()); System.out.println("Exist BO external pgm: "+o.getExtPrgId()); System.out.println("Exist User id: "+o.getUserId()); } */
UserExtrnRel _boUser = new UserExtrnRel(); //_boUser.setNtLogin(user.getNtLogin()); _boUser.setExtPrgId(BoExpPrg); _boUser.setBoUserId(boUser.getBoUserId()); _boUser.setBoUserPasswd(boUser.getBoPassword()); if (existNew.equals("E")){ _boUser.setUserId(user.getUserId()); System.out.println("UserId set is: "+user.getUserId()); }
newBoExtRel.add(_boUser); user.setUserExtrnRel(newBoExtRel);
// List the elements for (Iterator it=newBoExtRel.iterator(); it.hasNext(); ) { UserExtrnRel o = (UserExtrnRel) it.next(); System.out.println("BO userid :"+o.getBoUserId()); System.out.println("BO external pgm: "+o.getExtPrgId()); System.out.println("User id: "+o.getUserId()); }
} catch (Exception e) { log.error(e.getMessage(), e); throw new PandaServiceDAOHelperException(e.getMessage(), e); } finally { if (boClient != null) { boClient.endSession(); } } }
USer table UserId is 776 , BOuserid is 94351
1 AABLaLAAiAAAAGEAAC 776 M M g354098 g354098 g354098 06/08/2009 06/08/2009 94351 Gb429S_%D 1
Child table
UserId is updated as null
1 AABMMgAAiAAAAR0AAA NULL 3 94351 mL63y6_%D 37
Can anybody please help. I am stuck up here for the last 2 days
|