org.hibernate.TransientObjectException:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例
在我的项目中,我有 User
、Role
、UserRole
和 BloodGroup
实体。首先,我从数据库中获取List
并设置为User
。然后我将 User
和 Role
实体赋予 UserRole
。之后,我将 User
插入数据库,然后尝试插入 UserRole
,但出现错误。当我查看数据库时,BloodGroup
的 ID 未插入到 User
表中。
如果我选择列表中的第一个 BloodGroup
,则会收到错误消息。其他选项正常。
我上网查了一下,发现 cascade = CascadeType.ALL
,但这将相同的数据添加到 BloodGroup
中,这意味着我有更多 Arh+ BloodGroup
。
实体:
@Entity
@Table(name="USERS")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long userid;
@OneToMany(mappedBy="user")
private List<Userrole> userroles;
//bi-directional many-to-one association to Bloodgroup
@ManyToOne
@JoinColumn(name="BLOODGRUPID")
private Bloodgroup bloodgroup;
}
@Entity
public class Bloodgroup implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int bloodgroupid;
private String bloodgroupname;
@OneToMany(mappedBy="bloodgroup")
private List<User> users;
}
@Entity
public class Userrole implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long userroleid;
private Timestamp createddate;
private Timestamp deleteddate;
private String isactive;
//bi-directional many-to-one association to Role
@ManyToOne
@JoinColumn(name="ROLEID")
private Role role;
//bi-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="USERID")
private User user;
}
控制器:
user.setBloodgroup(bloodGroupImpl.getBloodGroupById(bGroup));
user.setUserid(userImpl.insertUserProfile(user));
userRoleImpl.insertUserRole(user,role);
DAO:
public void insertUserRole(User user, Role role) {
Session session =getHibernateTemplate().getSessionFactory().getCurrentSession();
Userrole uRole = new Userrole();
uRole.setIsactive("1");
uRole.setRole(role);
uRole.setUser(user);
session.save(uRole);
session.flush();
}
public void insertUserProfile(User user) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
session.save(user);
}
日志:
Hibernate:
insert
into
IU.Userrole
(userroleid, createddate, deleteddate, isactive, ROLEID, USERID)
values
(default, ?, ?, ?, ?, ?)
05.Şub.2012 19:23:29 com.sun.faces.application.ActionListenerImpl processAction
SEVERE: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.iu.eblood.model.Bloodgroup
javax.faces.el.EvaluationException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.iu.eblood.model.Bloodgroup
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:103)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:310)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
In my project I have User
, Role
, UserRole
and BloodGroup
entities. Firstly I take List<BloodGroup>
from DB and set to User
. Then I give User
and Role
entites to UserRole
. After that I insert User
to DB, then I try to insert UserRole
, but I get an error. When I look to DB, the ID of BloodGroup
isn't inserted in User
table.
If I choose the first BloodGroup
in the list, I get an error. Other options is normal.
I look at the internet, I found cascade = CascadeType.ALL
, but this add same data to BloodGroup
, which means I have more Arh+ BloodGroup
.
The entities:
@Entity
@Table(name="USERS")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long userid;
@OneToMany(mappedBy="user")
private List<Userrole> userroles;
//bi-directional many-to-one association to Bloodgroup
@ManyToOne
@JoinColumn(name="BLOODGRUPID")
private Bloodgroup bloodgroup;
}
@Entity
public class Bloodgroup implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int bloodgroupid;
private String bloodgroupname;
@OneToMany(mappedBy="bloodgroup")
private List<User> users;
}
@Entity
public class Userrole implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long userroleid;
private Timestamp createddate;
private Timestamp deleteddate;
private String isactive;
//bi-directional many-to-one association to Role
@ManyToOne
@JoinColumn(name="ROLEID")
private Role role;
//bi-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="USERID")
private User user;
}
Controller:
user.setBloodgroup(bloodGroupImpl.getBloodGroupById(bGroup));
user.setUserid(userImpl.insertUserProfile(user));
userRoleImpl.insertUserRole(user,role);
DAO:
public void insertUserRole(User user, Role role) {
Session session =getHibernateTemplate().getSessionFactory().getCurrentSession();
Userrole uRole = new Userrole();
uRole.setIsactive("1");
uRole.setRole(role);
uRole.setUser(user);
session.save(uRole);
session.flush();
}
public void insertUserProfile(User user) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
session.save(user);
}
Log:
Hibernate:
insert
into
IU.Userrole
(userroleid, createddate, deleteddate, isactive, ROLEID, USERID)
values
(default, ?, ?, ?, ?, ?)
05.Şub.2012 19:23:29 com.sun.faces.application.ActionListenerImpl processAction
SEVERE: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.iu.eblood.model.Bloodgroup
javax.faces.el.EvaluationException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.iu.eblood.model.Bloodgroup
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:103)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:310)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的问题将通过正确定义级联依赖关系或在保存引用的实体之前保存引用的实体来解决。正确定义级联确实很困难,因为它们的使用方式存在细微的变化。
以下是定义级联的方法:
在这种情况下,每次保存、更新、删除等用户角色时,关联的角色和用户也将被保存、更新...
同样,如果您的用例要求您不要修改更新 Userrole 时的 User 或 Role,然后在修改 Userrole 之前简单地保存 User 或 Role
另外,双向关系具有单向所有权。在这种情况下,用户拥有血型。因此,级联只会从User -> 进行。血型。同样,您需要将 User 保存到数据库中(附加它或使其非暂时性),以便将其与 Bloodgroup 关联。
your issue will be resolved by properly defining cascading depedencies or by saving the referenced entities before saving the entity that references. Defining cascading is really tricky to get right because of all the subtle variations in how they are used.
Here is how you can define cascades:
In this scenario, every time you save, update, delete, etc Userrole, the assocaited Role and User will also be saved, updated...
Again, if your use case demands that you do not modify User or Role when updating Userrole, then simply save User or Role before modifying Userrole
Additionally, bidirectional relationships have a one-way ownership. In this case, User owns Bloodgroup. Therefore, cascades will only proceed from User -> Bloodgroup. Again, you need to save User into the database (attach it or make it non-transient) in order to associate it with Bloodgroup.
我遇到了类似的问题,尽管我确保首先保存引用的实体,但它仍然因相同的异常而失败。
经过几个小时的调查后发现问题是因为引用实体的“版本”列为 NULL。
在我的特定设置中,我首先将它插入到 HSQLDB(这是一个单元测试)中,如下所示:
上面的问题是 hibernate 使用的版本列设置为 null,因此即使对象已正确保存, Hibernate 认为它未保存。当确保版本具有非空(在本例中为 1)值时,异常消失并且一切正常。
我将其放在这里以防其他人遇到同样的问题,因为这花了我很长时间才弄清楚,并且解决方案与上面的完全不同。
I had a similar problem and although I made sure that referenced entities were saved first it keeps failing with the same exception.
After hours of investigation it turns out that the problem was because the "version" column of the referenced entity was NULL.
In my particular setup i was inserting it first in an HSQLDB(that was a unit test) a row like that:
The problem of the above is the version column used by hibernate was set to null, so even if the object was correctly saved, Hibernate considered it as unsaved. When making sure the version had a NON-NULL(1 in this case) value the exception disappeared and everything worked fine.
I am putting it here in case someone else had the same problem, since this took me a long time to figure this out and the solution is completely different than the above.
我解决了这个问题,将 @Cascade 添加到 @ManyToOne 属性。
I Solved this problem adding @Cascade to the @ManyToOne attribute.
在我的例子中,在合并 o save 方法之前将引用的对象设置为 NULL 可以解决问题,在我的例子中,引用的对象是目录,不需要保存,因为在某些情况下我没有它甚至。
In my case setting the referenced object to NULL in my object before the merge o save method solve the problem, in my case the referenced object was catalog, that doesn't need to be saved, because in some cases I don't have it even.
下面是解决我的问题的解释,而不是通过引用对象传递保存的对象:
Instead of passing reference object passed the saved object, below is explanation which solve my issue: