Hi,
I am very new to Hibernate so require your help,please.
1. I have two tables mst_users and mst_user_role
2. When I insert the record in mst_users the id generated for this table should be stored as user_id (from mst_users) in mst_user_role_map with role_id(from mst_roles)
3. When I try for the following code
Code:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.prms.model.MstRoles;
import com.prms.model.MstUserRoleMap;
import com.prms.model.MstUsers;
import com.prms.util.SessionFactoryUtil;
import com.sun.org.apache.bcel.internal.generic.GETSTATIC;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
public class test {
public static void main(String[] args){
Session session = SessionFactoryUtil.currentSession();
DateFormat date = new SimpleDateFormat("yyyy-mm-dd" );
java.util.Date frmDate = null;
try {
frmDate = date.parse("2010-01-27");
System.out.println("dDate "+date.format(frmDate));
} catch (ParseException e) {
e.printStackTrace();
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Transaction tx = session.beginTransaction();
//create the object of user,role,userrolemap and userrolemap set
MstRoles mstRoles = new MstRoles();
MstUsers usersObj = new MstUsers();
MstUserRoleMap userRoleMapObj = new MstUserRoleMap();
Set<MstUserRoleMap> userRoleMap = new HashSet<MstUserRoleMap>();
//create the object of user
usersObj.setFname("Nitin"); //this.getFname()
usersObj.setMname("L");
usersObj.setLname("S");
usersObj.setShortName("NLS");
usersObj.setDob(10);
usersObj.setJoiningDate(10);
usersObj.setPAddress("P");
usersObj.setCAddress("P");
usersObj.setEmail("nls@gmail.com");
usersObj.setContactNo("2");
usersObj.setMobile("9");
usersObj.setUserId("123");
usersObj.setPassword("a");
usersObj.setStatus(1);
usersObj.setOfficeEmailId("a");
// String selectQuery = ("Select role.id,role.name,role.description,role.status from MstRoles role where role.id = :activeStatus");
// //Query query = session.createQuery(selectQuery).setString(
// // "activeStatus", "37");
// Query query = session.createQuery(selectQuery).setString(
// "activeStatus", "37");
mstRoles.setId(37);
mstRoles.setName("a");
mstRoles.setType("t");
mstRoles.setDescription("description");
mstRoles.setStatus(1);
System.out.println(mstRoles.getId());
userRoleMapObj.setAmdDate(new Date());
userRoleMapObj.setAmduserId(1);
userRoleMapObj.setMstRoles(mstRoles);
userRoleMapObj.setMstUsers(usersObj);
userRoleMapObj.setMstUserRoleMap(userRoleMapObj);
userRoleMap.add(userRoleMapObj);
usersObj.setMstUserRoleMaps(userRoleMap);
session.save(usersObj);
session.save(userRoleMapObj);
tx.commit();
}
}
I get the null error for set of userRoleMap as the user_role_id (pk of mst_user_role_map) is used in other tables as foreign key. The other tables are not at all usefull at this stage for me then how do I proceed store the user and user role mapping.
Thanks & Regards,
Avadhut Joshi