我有一个主表混合
和一个表卡片
,它们之间有一对一的关系。在添加卡时,我会下拉选择一张
混合
,卡上有一列混合id
。
在添加
卡时
我想将卡
保存在表中,其混合id
值等于从下拉列表中选择的混合id。
我使用的是spring hibernate,这是我在提交卡片表单时遇到的一个例外:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.terp.entity.BlendEntity
org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:275)
org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:295)
org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3403)
org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:520)
org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:230)
org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
$Proxy26.addCard(Unknown Source)
com.terp.controller.CardController.addCard(CardController.java:122)
com.terp.controller.CardController$$FastClassByCGLIB$$43e17ccc.invoke(<generated>)
net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:617)
com.terp.controller.CardController$$EnhancerByCGLIB$$4627162d_2.addCard(<generated>)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
从异常似乎是,它想保存选定的
混合
对象,但我不想保存混合,因为这是我的主表,我只想在我的卡片
表中引用它。
以下是java类:
BlendEntity.java
package com.terp.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Proxy;
@Entity
@Table(name="blends")
@Proxy(lazy=false)
public class BlendEntity {
@Id
@Column(name="id")
@GeneratedValue
private Integer id;
private String name;
private String code;
@Temporal(TemporalType.TIMESTAMP)
private Date created_at;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Date getCreated_at() {
return created_at;
}
public void setCreated_at(Date created_at) {
this.created_at = created_at;
}
}
CardEntity。JAVA
package com.terp.entity;
import java.util.Date;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Proxy;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;
import org.springframework.transaction.annotation.Transactional;
@Entity
@Table(name="cards")
@Proxy(lazy=false)
public class CardEntity {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne
@JoinColumn(name="division_id", referencedColumnName="id")
private DivisionEntity division;
private Integer card_series;
private String card_number;
private Integer shade;
@OneToOne
@JoinColumn(name="unit_of_qty_id", referencedColumnName="id")
private UnitEntity unit;
private Date card_date;
private Date issue_date;
private String cutomer_ref;
private String hl_number;
private String design_number;
@OneToOne
@JoinColumn(name="designWay_id", referencedColumnName="id")
private DesignWayEntity designWay;
/* @OneToOne
@JoinColumn(name="dyeingType_id", referencedColumnName="id")
private DyeingTypeEntity dyeingType;*/
private String dyeingType;
private Integer total_days;
private Date order_date;
private Date grey_date;
private Date target_date;
@OneToOne
@JoinColumn(name="qualityBase_id", referencedColumnName="id")
private QualityBaseEntity qualityBase;
@OneToOne
@JoinColumn(name="openFor_id", referencedColumnName="id")
private OpenForEntity openFor;
@OneToOne
@JoinColumn(name="width_id", referencedColumnName="id")
private WidthEntity width;
@OneToOne
@JoinColumn(name="finishType_id", referencedColumnName="id")
private FinishTypeEntity finishType;
@OneToOne
@JoinColumn(name="blend_id", referencedColumnName="id")
private BlendEntity blend;
private Float weight;
@OneToOne
@JoinColumn(name="merchant_id", referencedColumnName="id")
private MerchantEntity merchant;
@OneToOne
@JoinColumn(name="priority_id", referencedColumnName="id")
private PriorityEntity priority;
@OneToMany(fetch = FetchType.EAGER, mappedBy="card", cascade = CascadeType.ALL)
@Sort(type=SortType.NATURAL)
private SortedSet<ShadeEntity> shades = new TreeSet<ShadeEntity>();
private Float total_qty;
@Transactional
public SortedSet<ShadeEntity> getShades() {
return shades;
}
@Transactional
public void setShades(SortedSet<ShadeEntity> shades) {
this.shades = shades;
}
@Temporal(TemporalType.TIMESTAMP)
private Date created_at;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public DivisionEntity getDivision() {
return division;
}
public void setDivision(DivisionEntity division) {
this.division = division;
}
public Integer getCard_series() {
return card_series;
}
public void setCard_series(Integer card_series) {
this.card_series = card_series;
}
public String getCard_number() {
return card_number;
}
public void setCard_number(String card_number) {
this.card_number = card_number;
}
public Integer getShade() {
return shade;
}
public void setShade(Integer shade) {
this.shade = shade;
}
public UnitEntity getUnit() {
return unit;
}
public void setUnit(UnitEntity unit) {
this.unit = unit;
}
public Date getCard_date() {
return card_date;
}
public void setCard_date(Date card_date) {
this.card_date = card_date;
}
public String getCutomer_ref() {
return cutomer_ref;
}
public void setCutomer_ref(String cutomer_ref) {
this.cutomer_ref = cutomer_ref;
}
public String getHl_number() {
return hl_number;
}
public void setHl_number(String hl_number) {
this.hl_number = hl_number;
}
public String getDesign_number() {
return design_number;
}
public void setDesign_number(String design_number) {
this.design_number = design_number;
}
public DesignWayEntity getDesignWay() {
return designWay;
}
public void setDesignWay(DesignWayEntity designWay) {
this.designWay = designWay;
}
public String getDyeingType() {
return dyeingType;
}
public void setDyeingType(String dyeingType) {
this.dyeingType = dyeingType;
}
public Date getOrder_date() {
return order_date;
}
public void setOrder_date(Date order_date) {
this.order_date = order_date;
}
public Date getGrey_date() {
return grey_date;
}
public void setGrey_date(Date grey_date) {
this.grey_date = grey_date;
}
public Date getTarget_date() {
return target_date;
}
public void setTarget_date(Date target_date) {
this.target_date = target_date;
}
public QualityBaseEntity getQualityBase() {
return qualityBase;
}
public void setQualityBase(QualityBaseEntity qualityBase) {
this.qualityBase = qualityBase;
}
public OpenForEntity getOpenFor() {
return openFor;
}
public void setOpenFor(OpenForEntity openFor) {
this.openFor = openFor;
}
public WidthEntity getWidth() {
return width;
}
public void setWidth(WidthEntity width) {
this.width = width;
}
public FinishTypeEntity getFinishType() {
return finishType;
}
public void setFinishType(FinishTypeEntity finishType) {
this.finishType = finishType;
}
public BlendEntity getBlend() {
return blend;
}
public void setBlend(BlendEntity blend) {
this.blend = blend;
}
public Float getWeight() {
return weight;
}
public void setWeight(Float weight) {
this.weight = weight;
}
public Integer getTotal_days() {
return total_days;
}
public void setTotal_days(Integer total_days) {
this.total_days = total_days;
}
public MerchantEntity getMerchant() {
return merchant;
}
public void setMerchant(MerchantEntity merchant) {
this.merchant = merchant;
}
public PriorityEntity getPriority() {
return priority;
}
public void setPriority(PriorityEntity priority) {
this.priority = priority;
}
public Float getTotal_qty() {
return total_qty;
}
public void setTotal_qty(Float total_qty) {
this.total_qty = total_qty;
}
public Date getCreated_at() {
return created_at;
}
public void setCreated_at(Date created_at) {
this.created_at = created_at;
}
public Date getIssue_date() {
return issue_date;
}
public void setIssue_date(Date issue_date) {
this.issue_date = issue_date;
}
}
牌桌上的违禁品:
CONSTRAINT `fk_40` FOREIGN KEY (`blend_id`) REFERENCES `blends` (`id`),
在卡片中,如果我添加级联,它将在混合中添加新行:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="blend_id", referencedColumnName="id")
private BlendEntity blend;
您有持久性上下文问题。看看什么是持久性上下文。未保存的临时实例不在持久性上下文中。
TransientObject异常javadoc:
当用户将瞬态实例传递给期望持久实例的会话方法时引发。
>
不要把@Transactional
放在实体方法上。最佳实践是在服务(可能还有Dao)层上定义@Transactional
。此外,请检查服务/Dao/存储库配置和代码,确保这两个实体由相同的持久性上下文管理,例如,通过find()
ing混合ID引用的先前持久化混合实体,然后将其分配给卡。为了更好地理解实体、实体管理器、持久性上下文和事务是如何相互作用的,我从Mike Keith和Merrick Schincariol的Pro JPA 2第二版中摘录了以下要点。我与作者无关。
>
持久实体既有内存中的对象标识,也有由(主键)唯一标识的数据库表示(行)定义的数据库标识。
临时实体没有数据库表示,无论应用程序是否设置了实体的“唯一标识”字段。
Java内存模型不是事务性的。分离(或暂时)对象实体的状态不会自动与数据库同步。
如果持久性上下文参与事务,则托管实体的内存状态将与数据库同步。
分离的(暂时的)实体不能与任何需要托管实例的实体管理器操作一起使用。
实体管理器类型决定持久性上下文的生存期。
JPA定义了3种类型的实体管理器,每种都需要不同的持久化上下文管理:
容器管理实体管理器:
>
交易范围:
@PersistenceContext
//默认为type=PersistenceContextType。交易
扩展范围:
@PersistenceContext(type=PersistenceContextType.EXTENDED)
应用程序管理实体管理器:
close()
我试图在名为Person和Address的两个实体之间建立单向关系,同时保存Person(包含地址集合)获取组织。冬眠TransientObjectException:对象引用未保存的临时实例。 当我更改cascadeType=all时,子对象正在传播。但这里的问题是cascadeType=all Hibernate在删除所属实体时也会尝试删除子实体。我不希望发生这种情况,因为在很多关系中,子实体
我正在尝试将值更新到表中。如果新创建和保存的日期范围id工作正常。但是当试图更新现有日期范围id的值时,它会给我一个错误“org”。冬眠TransientObjectException:对象引用未保存的临时实例-在刷新之前保存临时实例:'。如何更新两种情况下的表值?
我会尽可能具体,所以我想只是要求litle帮助修改这个hbm.xml文件 我的hibernate映射中有这个文件 这是: 我发现一些指南说使用级联操作,当我得到 org.hibernate.瞬态ObjectException:对象引用一个未保存的瞬态实例-在刷新之前保存瞬态实例: 但是我不知道使用哪种方法,我知道cascade=“all”但是我不知道在这种情况下是否有效,我使用这种方法来保存/更新
我对JPA和manytoone的关系感到困惑,我有一个类“Judiction”,它有很多“Judidget”,当我试图将一个Judidge实例存储到postgres db中时,它抛出错误消息如下: 判断类: 还有我的服务课: 在我的数据库中,我有判断表和用户表,我需要为多通关系创建一个作为判断用户的表吗?
我在我的项目中使用Spring引导和Spring数据,我有两个类: 第二类是: 增加任务的方法: 当我试图添加一个新任务时,它在Json对象上面给出了一个错误: {"出发":"fff","到达":"ffff","isFreeWayEnable": false,"车站":[{"id": 1},{"id": 2}]}
下面是代码: 我看到了很多帖子,但这并没有解决我的问题。感谢您的帮助。这里的RoleLookup是一个静态表。以下是例外: 组织。冬眠TransientObjectException:对象引用未保存的临时实例-在刷新之前保存临时实例:com。德国商业银行。清理CCP领域用户角色。roleLookup-