我读了很多关于我的问题的文件,但建议不能解决我的问题,所以我被迫打开这个主题。
我的问题是,当我试图保存我的实体时,我遇到了以下错误。我添加了实体类。谢谢
保存代码
@Transactional
@Override
public void saveEmployee(EmployeeTable employeeToSave)
{
employeeDao.saveOrUpdate(employeeToSave);
}
员工表
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;
}
}
员工上传表
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);
}
}
调试非常困难,我认为您必须检查EmployeeTable实体中的“grading”对象,实际上它可能不为null,但它没有存储在数据库中,也没有在hibernate中持久化,当您保存EmployeeTable对象时,它会抛出“object references an unsaved transient instance”异常
当将CascadeType. PERSIST
与使用saveOrUpdate混合时,会引发此问题。PERSIST适用于持久化实体(JPA-专有)的操作。
有两种选择:
将以下CascadeType添加到集合字段:
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
级联型。PERSIST是JPA专有的,对PERSIST()有影响。更多信息:http://www.objectdb.com/api/java/jpa/CascadeType.在使用JPA的情况下,您应该将Dao更改为使用persist而不是saveOrUpdate。
employeeDao.persist(employeeToSave);
我对JPA和manytoone的关系感到困惑,我有一个类“Judiction”,它有很多“Judidget”,当我试图将一个Judidge实例存储到postgres db中时,它抛出错误消息如下: 判断类: 还有我的服务课: 在我的数据库中,我有判断表和用户表,我需要为多通关系创建一个作为判断用户的表吗?
当我尝试使用hibernate postgresql获取列表时,我收到以下错误 MyObject: 用户: 首先,我获取用户并将其保存在用户对象中: 然后,当我试图通过用户获取MyObject的列表时: 我收到 我做错了什么?
下面是代码: 我看到了很多帖子,但这并没有解决我的问题。感谢您的帮助。这里的RoleLookup是一个静态表。以下是例外: 组织。冬眠TransientObjectException:对象引用未保存的临时实例-在刷新之前保存临时实例:com。德国商业银行。清理CCP领域用户角色。roleLookup-
我有一个主表和一个表,它们之间有一对一的关系。在添加
我正在使用Hibernate,出现以下错误: - 我创建了一个实体的对象,我在其中执行了以下操作:
问题内容: 使用Hibernate保存对象时收到以下错误 问题答案: 你应该在集合映射中包括如果使用xml)或(如果使用注释)。 发生这种情况是因为你的实体中有一个集合,并且该集合具有一个或多个数据库中不存在的项目。通过指定上述选项,你可以告诉hibernate在保存其父项时将它们保存到数据库中。