休眠“对象引用了未保存的瞬态实例”问题

时间:2018-09-06 06:56:18

标签: java hibernate

我阅读了很多有关我的问题的文档,但是建议无法解决我的问题,所以我不得不打开这个主题。

我的问题是,当我尝试保存实体时,出现以下错误。 我添加了实体类。谢谢。

保存代码

 @Transactional
@Override
public void saveEmployee(EmployeeTable employeeToSave)
{
    employeeDao.saveOrUpdate(employeeToSave);
}

EmployeeTable

package de.adesso.kkBonus.model;

import com.google.common.collect.Lists;
import de.adesso.kkBonus.enums.*;
import de.adesso.kkBonus.util.BillConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.hibernate.annotations.Cascade;
import org.springframework.beans.BeanUtils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "employee_table")
@EqualsAndHashCode(of = {"id"})
@ToString(of = "id")
public class EmployeeTable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "deleted")
private boolean deleted;

@Column(name = "name", length = 200, nullable = false)
private String name;

@Column(name = "tckn", length = 11)
private String tckn;

@Column(name = "social_security_number")
private String socialSecurityNumber;

@Column(name = "address", length = 1000)
private String address;

@Column(name = "birthday")
private Date birthday;

@Column(name = "lob")
@Enumerated(EnumType.STRING)
private LobEnum lob;

@Column(name = "on_boarding_date", nullable = false)
private Date onBoardingDate;

@Column(name = "off_boarding_date")
private Date offBoardingDate;

@Column(name = "off_boarding_reason")
private String offBoardingReason;

@Column(name = "manager")
private boolean manager;

@Column(name = "created_by", length = 200, nullable = false)
private String createdBy;

@Column(name = "creation_date")
private Date creationDate;

@Column(name = "modified_by", length = 200, nullable = false)
private String modifiedBy;

@Column(name = "modified_date", updatable = false)
private Date modifiedDate;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "manager_fk", referencedColumnName = "id")
private EmployeeTable director;

@Column(name = "mail", length = 50, nullable = false)
private String mail;

@Column(name = "certificate", length = 1000)
private String certificate;

@Column(name = "billable")
private Boolean billable;

@Column(name = "outsource")
private Boolean outsource;

@Column(name = "location", length = 255)
private String location;

@Column(name = "gender")
@Enumerated(EnumType.STRING)
private GenderEnum gender;

@Column(name = "blood_type")
@Enumerated(EnumType.STRING)
private BloodTypeEnum bloodType;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "buddy", nullable = true)
private EmployeeTable buddy;

@Column(name = "contract_type")
@Enumerated(EnumType.STRING)
private ContractTypeEnum contractType;

@Column(name = "competence_center")
@Enumerated(EnumType.STRING)
private CompetenceCenterEnum competenceCenter;

@Column(name = "position")
@Enumerated(EnumType.STRING)
private PositionEnum position;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "hrbp", referencedColumnName = "id")
private EmployeeTable hrbp;

@Column(name = "education_status")
@Enumerated(EnumType.STRING)
private EducationStatusEnum educationStatus;

@Column(name = "military_status")
@Enumerated(EnumType.STRING)
private MilitaryStatusEnum militaryStatus;

@Column(name = "military_exempt_reason", length = 500)
private String militaryExemptReason;

@Column(name = "military_postponement_date")
private Date militaryPostponementDate;

@Column(name = "personal_phone_number", length = 20)
private String personalPhoneNumber;

@Column(name = "work_phone_number", length = 20)
private String workPhoneNumber;

@Column(name = "private_email", length = 50)
private String privateEmail;

@Column(name = "emergency_person_name", length = 120)
private String emergencyPersonName;

@Column(name = "emergency_person_phone_number", length = 11)
private String emergencyPersonPhoneNumber;

//Sicil No
@Column(name = "registry_number", length = 50)
private String registryNumber;

@Column(name = "continuity", length = 3)
private Integer continuity;

@Column(name = "iban", length = 50)
private String iban;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "graduation_id")
private GraduationTable graduation;

@OneToOne(cascade = CascadeType.ALL, mappedBy = "employeeTable", orphanRemoval = true, fetch = FetchType.LAZY)
private UserTable userTable;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "employee", orphanRemoval = false, fetch = FetchType.LAZY)
private List<InventoryTable> inventoryTables;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "employee", orphanRemoval = true, fetch = FetchType.LAZY)
private List<ResearchAndDevelopmentDetailsTable> researchAndDevelopmentDetailsTables;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "employeeTable", orphanRemoval = true, fetch = FetchType.LAZY)
private List<EmployeeUploadTable> employeeUploadTables = Lists.newArrayList();

@Lob
@Column(name = "image", columnDefinition = "MEDIUMBLOB")
private String image;

@Transient
public boolean isMale() {
    return gender.equals(GenderEnum.MALE);
}

public EmployeeTable createCopy() {
    EmployeeTable target = new EmployeeTable();
    BeanUtils.copyProperties(this, target);
    if (this.getGraduation() != null) {
        target.setGraduation(this.getGraduation().createCopy());
    }
    List listDocuments = new LinkedList();
    if (employeeUploadTables != null) {
        for (EmployeeUploadTable employeeUploadTable : employeeUploadTables) {
            listDocuments.add(employeeUploadTable.createCopy());
        }
        target.getEmployeeUploadTables().clear();
        target.getEmployeeUploadTables().addAll(listDocuments);
    }

    return target;
}

public boolean isOffBoarded() {
    return offBoardingDate != null;
}


public EmployeeUploadTable getResidenceCertificate() {
    if (employeeUploadTables != null) {
        for (EmployeeUploadTable employeeUploadTable : employeeUploadTables) {
            if (DocumentTypeEnum.RESIDENCE_CERTIFICATE.equals(employeeUploadTable.getContentType())) {
                return employeeUploadTable;
            }
        }
    }
    return null;
}

public EmployeeUploadTable getBirthCertificate() {
    if (employeeUploadTables != null) {
        for (EmployeeUploadTable employeeUploadTable : employeeUploadTables) {
            if (DocumentTypeEnum.BIRTH_CERTIFICATE.equals(employeeUploadTable.getContentType())) {
                return employeeUploadTable;
            }
        }
    }
    return null;
}

public EmployeeUploadTable getDiploma() {
    if (employeeUploadTables != null) {
        for (EmployeeUploadTable employeeUploadTable : employeeUploadTables) {
            if (DocumentTypeEnum.DIPLOMA.equals(employeeUploadTable.getContentType())) {
                return employeeUploadTable;
            }
        }
    }
    return null;
}

public EmployeeUploadTable getMilitaryStatusCertificate() {
    if (employeeUploadTables != null) {
        for (EmployeeUploadTable employeeUploadTable : employeeUploadTables) {
            if (DocumentTypeEnum.MILITARY_STATUS_CERTIFICATE.equals(employeeUploadTable.getContentType())) {
                return employeeUploadTable;
            }
        }
    }
    return null;
}

public EmployeeUploadTable getHealthReport() {
    if (employeeUploadTables != null) {
        for (EmployeeUploadTable employeeUploadTable : employeeUploadTables) {
            if (DocumentTypeEnum.HEALTH_REPORT.equals(employeeUploadTable.getContentType())) {
                return employeeUploadTable;
            }
        }
    }
    return null;
}

public EmployeeUploadTable getWorkCertificate() {
    if (employeeUploadTables != null) {
        for (EmployeeUploadTable employeeUploadTable : employeeUploadTables) {
            if (DocumentTypeEnum.WORK_CERTIFICATE.equals(employeeUploadTable.getContentType())) {
                return employeeUploadTable;
            }
        }
    }
    return null;
}

public EmployeeUploadTable getPayroll() {
    if (employeeUploadTables != null) {
        for (EmployeeUploadTable employeeUploadTable : employeeUploadTables) {
            if (DocumentTypeEnum.PAYROLL.equals(employeeUploadTable.getContentType())) {
                return employeeUploadTable;
            }
        }
    }
    return null;
}

public EmployeeUploadTable getCriminalRecord() {
    if (employeeUploadTables != null) {
        for (EmployeeUploadTable employeeUploadTable : employeeUploadTables) {
            if (DocumentTypeEnum.CRIMINAL_RECORD.equals(employeeUploadTable.getContentType())) {
                return employeeUploadTable;
            }
        }
    }
    return null;
}

public void addEmployeeUpdateTable(List<EmployeeUploadTable> employeeUploadTableList) {
    for (EmployeeUploadTable employeeUploadTable : employeeUploadTableList) {
        employeeUploadTable.setEmployeeTable(this);
        this.getEmployeeUploadTables().add(employeeUploadTable);
    }
}

public Boolean isAllDocumentsUploaded(){
    if(this.getBirthCertificate() == null || this.getResidenceCertificate() == null || this.getDiploma() == null
            || this.getMilitaryStatusCertificate() == null || this.getHealthReport() == null
            || this.getWorkCertificate() == null || this.getPayroll() == null || this.getCriminalRecord() == null){
        return Boolean.FALSE;
    }
    return Boolean.TRUE;
}

}

EmployeeUploadTable

 package de.adesso.kkBonus.model;

 import de.adesso.kkBonus.enums.DocumentTypeEnum;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.ToString;
 import org.hibernate.annotations.Cascade;
 import org.springframework.beans.BeanUtils;

import javax.persistence.*;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "employee_upload_table")
@ToString(of = "id")
public class EmployeeUploadTable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "employee_fk")
private EmployeeTable employeeTable;

@Column(name = "name")
private String name;

@Column(name = "content_type")
@Enumerated(EnumType.STRING)
private DocumentTypeEnum contentType;

@Column(name = "content")
private byte[] content;

@Transient
public EmployeeUploadTable createCopy() {
    EmployeeUploadTable target = new EmployeeUploadTable();
    BeanUtils.copyProperties(this, target);
    return target;
}

public EmployeeUploadTable(DocumentTypeEnum documentTypeEnum) {
    this.setContentType(documentTypeEnum);
}
}
  

org.springframework.dao.InvalidDataAccessApiUsageException:对象   引用未保存的瞬时实例-保存瞬时实例   刷新之前:de.adesso.kkBonus.model.EmployeeTable;嵌套的   org.hibernate.TransientObjectException例外:对象引用   未保存的瞬态实例-在保存瞬态实例之前   冲洗:位于以下位置的de.adesso.kkBonus.model.EmployeeTable   org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:188)     在   org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:681)     在   org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:563)     在   org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)     在   org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)     在   org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478)     在   org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272)     在   org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)     在   org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)     在com.sun.proxy。$ Proxy69.saveEmployee(未知来源)处   de.adesso.kkBonus.controller.impl.EmployeeControllerImpl.saveEmployee(EmployeeControllerImpl.java:219)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:737)在   javax.el.BeanELResolver.invoke(BeanELResolver.java:467)在   javax.el.CompositeELResolver.invoke(CompositeELResolver.java:246)在   com.sun.el.parser.AstValue.invoke(AstValue.java:228)位于   com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)     在   com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)     在   javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:147)     在javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)     在   javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)     在javax.faces.component.UICommand.broadcast(UICommand.java:300)在   javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)     在   javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)     在   com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)     在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)处   com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)     在javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)处   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在   org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在   de.adesso.kkBonus.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:38)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在de.adesso.kkBonus.filter.LoginFilter.doFilter(LoginFilter.java:36)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在   de.adesso.kkBonus.filter.AjaxExceptionFilter.doFilter(AjaxExceptionFilter.java:42)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)     在   org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)     在   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:603)     在   org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)     在   java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)     在java.lang.Thread.run(Thread.java:748)造成原因:   org.hibernate.TransientObjectException:对象引用未保存的   瞬态实例-在刷新之前保存瞬态实例:   de.adesso.kkBonus.model.EmployeeTable位于   org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:294)     在org.hibernate.type.EntityType.getIdentifier(EntityType.java:537)     在org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:311)     在org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:321)     在org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:294)处   org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:4243)     在   org.hibernate.event.internal.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:546)     在   org.hibernate.event.internal.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:232)     在   org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:159)     在   org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:231)     在   org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102)     在   org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55)     在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)     在   org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)     在   org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)     在   org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)     在   org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:555)     ...还有61

2 个答案:

答案 0 :(得分:0)

CascadeType.PERSIST与使用saveOrUpdate混合使用时,会出现此问题,该@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE) 可用于持久化实体(JPA专有)的操作。

有两个选项:

选项1:

将以下CascadeType添加到收集字段:

employeeDao.persist(employeeToSave);

选项2:

CascadeType.PERSIST是JPA专有的,对persist()有影响。 有关更多信息:http://www.objectdb.com/api/java/jpa/CascadeType。如果使用JPA,则应将Dao更改为使用persist,而不是saveOrUpdate。

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.setVisibility(childView.getId(), View.VISIBLE);
constraintSet.applyTo(constraintLayout);

答案 1 :(得分:0)

调试非常困难,我认为您必须在EmployeeTable实体中检查“ graduation”对象,实际上它可能不为null,但尚未存储在数据库中,并且在保存EmployeeTable对象时不会持久保存,它是抛出“对象引用了未保存的瞬态实例”异常