gpt4 book ai didi

java - org.hibernate.TransientObjectException : object references an unsaved transient instance - save the transient instance before flushing

转载 作者:IT老高 更新时间:2023-10-28 13:48:59 26 4
gpt4 key购买 nike

在我的项目中,我有 User , Role , UserRoleBloodGroup实体。首先我拿List<BloodGroup>来自 DB 并设置为 User .那我给UserRole实体到 UserRole .之后我插入 User到 DB,然后我尝试插入 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;

}

Controller :

user.setBloodgroup(bloodGroupImpl.getBloodGroupById(bGroup));
user.setUserid(userImpl.insertUserProfile(user));
userRoleImpl.insertUserRole(user,role);

道:

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)

最佳答案

您的问题将通过正确定义级联依赖关系或在保存引用的实体之前保存引用的实体来解决。定义级联非常棘手,因为它们的使用方式存在所有细微的变化。

这是定义级联的方法:

@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(cascade = CascadeType.ALL)
@JoinColumn(name="ROLEID")
private Role role;

//bi-directional many-to-one association to User
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="USERID")
private User user;

}

在这种场景下,每次保存、更新、删除等Userrole时,关联的Role和User也会被保存、更新……

同样,如果您的用例要求您在更新用户角色时不修改用户或角色,那么只需在修改用户角色之前保存用户或角色

此外,双向关系具有单向所有权。在这种情况下,用户拥有 Bloodgroup。因此,级联只会从 User -> Bloodgroup 开始。同样,您需要将 User 保存到数据库中(附加或使其成为非 transient ),以便将其与 Bloodgroup 关联。

关于java - org.hibernate.TransientObjectException : object references an unsaved transient instance - save the transient instance before flushing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9149536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com