我有一个主表Blends
和一个表cards
,它们之间有一对一的关系。在添加card
时我会选择一个blend
下拉列表,但卡片上有blend_id
列。
添加card
后,我想将card
保存在表格中,blend_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)
从异常似乎它想要保存选定的blend
对象,但我不想保存混合,因为这是我的主表,我只想在我的cards
表中引用它。
这些是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;
答案 0 :(得分:2)
您有持久性上下文问题。见What is Persistence Context。 未保存的瞬态实例不在持久性上下文中。
TransientObjectException javadoc:
当用户将瞬态实例传递给Session方法时抛出 期望一个持久的实例。
如果关系真的是@OneToOne
,请考虑重新分解
通过在Card表中定义Blend列来构建模式。 @OneToOne#optional和@JoinColumn#nullable都默认为true。但是,@OneToOne
关系的意图是目标实体(行)不被多个引用实体共享。允许null Blend外键意味着 no Card可以共享许多Blend。
不要将@Transactional
放在实体方法上。 Best practice是在服务(也许是Dao)层上定义@Transactional
。此外,检查您的Service / Dao / Repository配置和代码,确保两个实体都由相同的持久性上下文管理,例如,find()
@PersistenceContext
混合ID引用的先前持久的Blend实体,然后将其分配给卡。为了更好地理解实体,实体管理器,持久性上下文和事务如何交互,我从Pro JPA 2, 2nd Edition by Mike Keith and Merrick Schincariol中删除了以下要点。我与作者无关。
持久化实体具有内存中对象标识和由(主)密钥唯一标识的数据库表示(行)定义的数据库标识。
瞬态实体没有数据库表示,无论应用程序是否设置了实体&#34;&#34;独特的身份&#34;字段(一个或多个)。
Java内存模型不是事务性的。分离(或瞬态)对象实体的状态不会与DB自动同步。
如果持久化上下文参与事务,则托管实体的内存中状态将与数据库同步。
分离(临时)实体不能与需要托管实例的任何实体管理器操作一起使用。
实体管理器类型确定持久化上下文的生命周期。
JPA定义了3种类型的实体管理器,每种类型都需要不同的持久化上下文管理:
容器管理的EntityManager:
交易范围:
type=PersistenceContextType.TRANSACTION
//默认为@PersistenceContext(type=PersistenceContextType.EXTENDED)
扩展范围:
close()
应用程序管理的EntityManager:
EntityManagerFactory.createEntityManager()
。 @PersistenceUnit
。EntityManagerFactory
。