Insert issue using ibatis

248 views
Skip to first unread message

PP

unread,
Apr 11, 2013, 2:29:31 AM4/11/13
to mybati...@googlegroups.com
I have 2 tables Employee and EmployeePosition (This has AUTOINCREMENT Field PositionId) Table. I am trying to insert values into table via Ibatis in a single save method of DAO.

Employee.XML

  <insert id="insertEmployeeRecord" parameterType="Employee" >
    insert into Employee (EmpNum,EmpName,ResidentRegNum) values
     (#{empNumber},#{empName},#{ssn});
  </insert>

 <insert id="insertEmployeePosition" parameterType="EmployeePosition" useGeneratedKeys="true"  keyProperty="positionId">
    insert into EmployeePosition (Position1,Position2) values
     (#{position1},#{position2});
  </insert>

Model class
public class Employee {
private String empName;
private int empNumber;
private String ssn;
private String ssn1;
        private EmployeePosition employeePosition;
}
public class EmployeePosition{
private int positionId;
private String position1;
private String position2;
}

DAO

public void saveEmployeeRecord(Employee empl)
throws SQLException {
SqlSession session = sqlSessionFactory.openSession();
 
empl.setEmpNumber(empl.getEmpNumber());
empl.setEmpName(empl.getEmpName());
empl.setDateOfBirth(empl.getDateOfBirth());
empl.setUserId(empl.getUserId());
empl.setUserPW(empl.getUserPW());
empl.setConfirmPW(empl.getConfirmPW());
empl.setWeddingAnvy(empl.getWeddingAnvy());
empl.setSingle(empl.isSingle());
empl.setSsn(empl.getSsn());
empl.setSsn1(empl.getSsn1());
EmployeePosition empPos=new EmployeePosition();
empl.setEmployeePosition(empPos);
session.insert("Employee.insertEmployeePosition", empPos);

 empPos.setPosition1(empPos.getPosition1());
 empPos.setPosition2(empPos.getPosition2());
session.insert("Employee.insertEmployeeRecord", empl);
session.commit(true);
session.rollback();
session.close();
//return workplaceDetail;
}
Unit Test case:
@Test
public void testInsertEmployee() throws SQLException{
Employee actual = new Employee();
actual.setEmpNumber(783);
actual.setEmpName("Hayes john");
actual.setSsn("05E-0965-084G");

EmployeePosition ep= new EmployeePosition();
ep.setPosition1("Dy Manager");
ep.setPosition2("Sr.Manager");
employeeDaoImpl.saveEmployeeRecord(actual);
assertEquals(10, actual.getEmpNumber());
Employee expected = employeeDaoImpl.geEmployeeById(actual.getEmpNumber()); //id = 21
assertEquals(actual, expected);
assertNotSame(actual, expected);
}

When i run unit test i get values insert to employee table but not to EmployeesPosition table. Bleow is output i am showing
DEBUG [main] - ==>  Executing: insert into EmployeePosition (Position1,Position2) values (?,?); 
DEBUG [main] - ==> Parameters: null, null
DEBUG [main] - ==>  Executing: insert into Employee (EmpNum,EmpName,ResidentRegNum) values (?,?,?); 
DEBUG [main] - ==> Parameters: 783(Integer), Hayes john(String), 05E-0965-084G(String),

Please let me know where i am going wrong.

Frank Martínez

unread,
Apr 11, 2013, 10:12:51 AM4/11/13
to mybati...@googlegroups.com
Hi, I am not trying to be hard, but... have you read your code?

1. Why do you re-assign every property to itself?
2. You are saving the record before setting its properties, what do you expect?

Don't get me wrong, but this is disgusting.

Frank.


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Frank D. Martínez M.

PP

unread,
Apr 11, 2013, 9:04:22 PM4/11/13
to mybati...@googlegroups.com
Actually i am new to java .... I am trying to learn. 

I am trying to save record into database i don know where actually issue is happening....Please guide me....

Tim

unread,
Apr 11, 2013, 9:17:45 PM4/11/13
to mybati...@googlegroups.com
PP given that you are new to mybatis (and java) I think you should start with this tutorial.


I only briefly looked at it but it seems like a decent and simple start
Just doing part 1 should be enough for you.


PP

unread,
Apr 11, 2013, 9:39:40 PM4/11/13
to mybati...@googlegroups.com
Thanks. I am fine with single insert save. what if my GUI contains a fields that needs to be inserted into multiple table like my above example. In above example i have Employee and employee position tables with 2 insert statement and i have model where EmployeePosition is referenced in Employee Class. I am using DAO to save those to insert...

PP

unread,
Apr 11, 2013, 9:59:56 PM4/11/13
to mybati...@googlegroups.com
After save i should be able to get record by using below query

  select distinct Employee.EmpNum,Employee.[EmpName],Employee.[ResidentRegNum],EmployeePosition.[Position1],
EmployeePosition.[Position2],Employee.[DOJ],Employee.[LWD],
Employee.[Weddinganvy],Employee.MartialStatus from Employee
inner join  [EmployeePosition] on  Employee.[PositionId]=[EmployeePosition].[PositionId]
Reply all
Reply to author
Forward
0 new messages