i want to save group_code into user table, but why idGroup is saved ?
I have design class group user like this,
how to i can save group_code into table user ?
Code:
@Entity
@Table(name="t_groupuser",
uniqueConstraints = {@UniqueConstraint(
columnNames={"GROUP_CODE",
"PROG_NAME"
})}
)
public class GroupUser implements java.io.Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID_GROUP")
private int idGroup;
@Column(name="GROUP_CODE", nullable=true,unique=false,length=20)
private String groupCode;
@Column(name="DESCRIPTION", nullable=true,unique=false,length=35)
private String description;
@Column(name="PROG_NAME", nullable=true,unique=false,length=20)
private String groupCode;
Code:
@Entity
@Table(name="t_user",
uniqueConstraints = {@UniqueConstraint(
columnNames={"USER_CODE",
"PROG_NAME"
})}
)
public class User implements java.io.Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID_USER")
private int idUser;
@Column(name="USER_CODE",nullable=false,unique=false, length=15)
private String userCode;
@Column(name="USER_NAME",nullable=false,unique=false, length=25)
private String userName;
@ManyToOne()
@JoinColumn(name="GROUP_CODE")
private GroupUser groupUser;
//get GroupCode
Code:
protected GroupUser getGroupCode(String description){
startOperation();
GroupUser groupUser=null;
try {
groupUser= (GroupUser) con.session.createQuery("from GroupUser where description = :description ")
.setParameter("description",description)
.uniqueResult();
con.session.flush();
}catch(Exception e){e.printStackTrace();
}
con.closeSession();
return groupUser;
}
//..
..
..
show description of group user in combobox
//
//get group code
groupUser = new GroupUser();
groupUserDao=new GroupUserDao();
groupUser=groupUserDao.getGroupCode(cmbGroupUser.getSelectedItem().toString())
Code:
//save data
user = new User();
userDao = new UserDao();
user.setUserCode(txtUserCode.getText().trim());
user.setUserName(txtName.getText().trim());
user.setGroupUser(groupUser);
session.save(user);
Thanks for your help
|