java - org.hibernate.id.IdentifierGenerationException : ids for this class must be manually assigned before calling save(): entity. PatientEntity

标签 java hibernate

我正在 Hibernate 中做一个多对一的程序,当我在 DAO 中编写以下代码时:

sessionFactory = HibernateUtility.createSessionFactory();
            session = sessionFactory.openSession();
           PatientEntity patientEntity=new PatientEntity();
           DoctorEntity doctorEntity=(DoctorEntity) session.get(DoctorEntity.class, doctorId);
         System.out.println(doctorEntity);
           if (doctorEntity!= null) 
           {
               System.out.println("hello");
                patientEntity.setId(patient.getId());
                patientEntity.setName(patient.getName());
                patientEntity.setAge(patient.getAge());
                patientEntity.setPhoneNumber(patient.getPhoneNumber());
                patientEntity.setDoctor(doctorEntity);
            }

            session.getTransaction().begin();
            session.persist(patientEntity);
            session.getTransaction().commit();t the 

我收到以下异常:

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): entity.PatientEntity

我的实体类:

@Entity
@Table(name="Doctor")
public class DoctorEntity {
    @Id
    @Column(name="id")
    private String id;
    private String name;
    private String phoneNumber;
    private String address;


    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

@Entity
@Table(name="Patient")
public class PatientEntity {

    @Id
    private Integer id;
    private String name;
    private String phoneNumber;
    private Integer age;
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="id",unique=true,insertable=false,updatable=false)
    private DoctorEntity doctor;
    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 getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public DoctorEntity getDoctor() {
        return doctor;
    }
    public void setDoctor(DoctorEntity doctor) {
        this.doctor = doctor;
    }
}

我已经通过不同的链接,但无法将我的问题与其关联起来。 Hibernate Many-To-One Relationship without Primary Key or Join Table

Repeated column in mapping for entity (should be mapped with insert="false" update="false")

Another Repeated column in mapping for entity error

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity

感谢任何帮助!

最佳答案

您可能必须指定@GenerateValue(strategy=GenerationType.AUTO)或使用您自己的自定义生成器。

关于java - org.hibernate.id.IdentifierGenerationException : ids for this class must be manually assigned before calling save(): entity. PatientEntity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190933/

相关文章:

java - 如何将 Pentaho 6.0 连接到 Hadoop-Hive

Java Jinput : rescan/reload controllers

hibernate - 如何使用 Spring Security、Hibernate 和行级 ACL 进行分页

mysql - 如何在不牺牲良好性能的情况下搜索 playframework JPA 实体?

java - 应对 MySQL 模式不断变化的需求的策略?

java - 无法编译 Java 代码 - "class file has wrong version 52.0, should be 48.0"

java - 在System.java源码中,标准输入、输出和错误流都被声明为final并初始化为null?

java - 使用 Spring Data JPA @LastModifiedDate 从审计中排除某些字段

java - 如何在android中的文本加密/解密中正确地将String转换为byte[]? (错误 - WRONG_FINAL_BLOCK_LENGTH 和 IllegalBlockSizeException)

java - hibernate 3.5.x : NoSuchMethodError: javax. persistence.OneToMany.orphanRemoval