I made small changes to the code
User.java
------------
package login;
/*import java.util.Set;
import java.util.HashSet;*/
import java.util.List;
import java.util.ArrayList;
public class User {
/** identifier field */
private Long id;
/** persistent fields */
private String userName;
private String userPassword;
private List usersalary = new ArrayList();
/** default constructors */
public User() {
}
public List getUsersalary() {
return usersalary;
}
public void setUsersalary(List usersalary) {
this.usersalary = usersalary;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return this.userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public void addChild(UserSalary child) throws IllegalArgumentException { if (child == null) { throw new IllegalArgumentException("Child may not be null"); } child.setUsers(this); usersalary.add(child); } }
UserSalary.java :
----------------------
package login;
import java.util.*;
public class UserSalary {
/** identifier field */
private Long id;
/** persistent fields */
private Long salary;
/** foreign fields */ private User users;
/** foreign fields */
private Long userid;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getSalary() {
return salary;
}
public void setSalary(Long salary) {
this.salary = salary;
}
public User getUsers() {
return users;
}
public void setUsers(User users) {
this.users = users;
}
public Long getUserid() {
return userid;
}
public void setUserid(Long user_id) {
this.userid = user_id;
}
}
I m able to list the users vth multiple salaries... But while inserting a user i need to insert users salary into the salary table too.. But while inserting the insert statement to usersalary table does not execute givin the error below :
Insert function :
---------------------
public void insert (User user,UserSalary userSalBean)throws Exception {
// Fire up Hibernate
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
// Open Session
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
user.addChild(userSalBean);
session.saveOrUpdate(user);
tx.commit();
session.flush();
session.close();
}catch (RuntimeException e) {
if (tx != null)
tx.rollback();
throw e; // or display error message
}
//session.getTransaction().commit();
}
Error :
-------------
Hibernate: select max(USER_ID) from users
Hibernate: insert into users (USER_NAME, USER_PASSWORD, USER_ID) values (?, ?, ?)
Hibernate: update usersalary set USER_ID=? where PK_USERSALARY_ID=?
Apr 20, 2006 4:30:05 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.TransientObjectException: login.UserSalary
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:71)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:
697)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1037
)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:29
7)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at login.UserInsertHibernate.insert(UserInsertHibernate.java:28)
at org.apache.jsp.addEditUserController_jsp._jspService(addEditUserController_jsp.java:89)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:688)
at java.lang.Thread.run(Thread.java:595)
|