当前位置: 首页 > 知识库问答 >
问题:

Java与Hibernate的外键使用

仉高昂
2023-03-14

我正试图开发一个web应用程序,我想知道是否有一种方法可以在不编写大量代码的情况下利用外键。

我的培训。java

@Entity
public class Trainees {

    @Id
    @GeneratedValue
    private int traineesID;
    private int groupsID;
    @ManyToOne
    @JoinColumn(name = "status_trainee")
    private String status_TraineeID;
    private int customersID;
    private String name;
    private String surname;
    private String phoneDetails;
    private String email;

    public Trainees(){

    }

    public Trainees(String name, String surname, String phoneDetails, String email, int id, int groupsID, String status_TraineeID, int customersID) {
        super();
        this.name = name;
        this.surname = surname;
        this.email = email;
        this.phoneDetails = phoneDetails;
        this.groupsID = groupsID;
        this.status_TraineeID = status_TraineeID;
        this.customersID = customersID;
    }

    //getters and setters

    @Override
    public boolean equals(Object object) {
        if (object instanceof Trainees){
            Trainees contact = (Trainees) object;
            return contact.traineesID == traineesID;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return traineesID;
    }
}

status_trainee.java

@Entity
public class Status_Trainee {

    @Id
    @GeneratedValue
    private int status_traineeID;
    private String value;

    public Status_Trainee(){

    }

    public Status_Trainee(String value, int id) {
        super();
        this.value = value;
    }

    //getters and setters

    @Override
    public boolean equals(Object object) {
        if (object instanceof Status_Trainee){
            Status_Trainee value = (Status_Trainee) object;
            return value.status_traineeID == status_traineeID;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return status_traineeID;
    }
}

错误:由:org.hibernate.annotationexception:uaicontacts.model.trainees.status_traineeid上的@onetoone或@manytoone引用了未知实体:String引起

谢谢所有的帮助!

共有1个答案

程景胜
2023-03-14

您应该在Strainee中添加对StatusTrainee的引用,并用OneToMany、ManyToOne或OneToone对其进行注释。根据哪种关系,你需要一个statestrainee的列表,或者只是一个statestrainee。

提示:不要在类名中使用下划线。

 类似资料:
  • 我有一些实体: 当我试图保存新的cbonus记录时,出现异常: org.postgresql.util.PSQLException: ERROR: null值在列"bank_id"的关系"cBonus"违反了非空约束详细信息:失败的行包含(773, gp3, null, null, f)。 和查询 DEBUG 24817-[nio-8080-exec-4]org . hibernate . SQL

  • 问题内容: 目标:我想在ImportJob中使用importJobId作为分配表ID的外键,这样,当我们那时具有importJobId时,那么我们只能在ID中分配ID,因为没有Job时就无法进行任何分配。 ImportJob表的复合主键为[ORGID,IMPORTJOBTYPE],正在尝试使用以下方式在hibernate状态下创建外键关系: 在Allocation.hbm.xml中无法解决,并收到

  • 我的数据库技能有点生疏,所以对我来说学习Hibernate有点难。 我一直在阅读一对一、一对多和多对多,简而言之,这是我年轻时的理解: 一对一:-表A链接到表B中的另一行,表A中的行数必须等于表B中的行数。 一对多:-每家公司可以有很多员工。 多对多:-许多医生可以拥有/分享不同的病人。(无意双关语)。 假设我有以下表格,对于殡仪员客户模型 [id[pk]、全名(char)、年龄(int)、地址(

  • 我有一个实体,叫做FatRabbitCarrot: 而且很管用。现在上面的类已经替换了字段名,但是结构和我们的存储库是一样的。 然后我尝试添加一个新实体,该实体在上面的类中有一个外键。让我们把这门课叫做NutToffee。FatRabbitCarrot与这个新实体有一对一的关系,而实体本身应该有一对一的关系: 对我来说,这似乎是一个有效的类。但看起来不像。我们正在使用Java8、Hibernate

  • 我可以这样做吗?或者有没有其他更好的方法来处理这种情况

  • 我有这个型号 而我有这个方法 但是hibernate没有设置实际地址id和注册地址id(它是OneTONE) Hibernate:插入客户(名字、姓氏、中间名、性别)值(?、、?、?)2021-03-18 14:01:58.340警告12836---[nio-8080-exec-1]o.h.发动机。jdbc。spi。SqlExceptionHelper:SQL错误:0,SQLState:23502