简体   繁体   English

JPA 实体与外键的多对一双向关系的最佳方式

[英]JPA Entity best way for ManyToOne Bidirectional Relationship with Foreign Key

Considering the below table structure and relation.考虑下表结构和关系。

Teacher
id(pk),
tname,
subject_id(fk),
subject_name

Subject
sid(pk),
sname,
subject_learning_curve

TEACHER(owner of relationship) to SUBJECT Many to One, bidirectional relationship TEACHER(关系所有者)到 SUBJECT 多对一,双向关系

I used persistence tool window here in intellij.我在 intellij 中使用了持久性工具window Which based on the data source generates the Entities automatically for all the relationships in DB.它基于数据源自动为数据库中的所有关系生成实体。

In the entity it is generated like this在实体中它是这样生成的

@Entity
//using lombok for getters and setters
public class TEACHER{
     
    @Id
    @GeneratedValue
    private Long id;
    private String tname;
    private long subject_id;
    private String subject_name;

    @ManyToOne
    @JoinColumn(name = "subject_id",referencedColumnName="sid")
    private Subject subjectBySid;    //is this fine to have a reference like this
}

OR 2nd approach below或下面的第二种方法

@Entity
    //using lombok for getters and setters
    public class Teacher{
         
        @Id
        @GeneratedValue
        private Long id;
        private String tname;
        @ManyToOne
        @JoinColumn(name = "subject_id",referencedColumnName="sid")
        private Subject subject;    //or just this it will suffice all use case,like if i just want to read Teacher data and just the sid not the whole subject row
    }

Similarly Subject will also have reference pointing back to Teacher table.同样,主题也将具有指向教师表的参考。

Based on below points,can someone provide insights.基于以下几点,有人可以提供见解。

  1. Which is ideal and correct out of the above two ways of writing a entity.在上述两种编写实体的方式中,哪种方式是理想且正确的。
  2. From Hibernate and JPA perspective which is more efficient.从 Hibernate 和 JPA 的角度来看,哪个更高效。
  3. Will there be any difference on the sql generated based on what approach i chose to write my entity.根据我选择编写实体的方法生成的 sql 会有什么不同吗?
  4. If i take 2nd approach will it be more easier to query and save records to DB or to retrieve.如果我采用第二种方法,查询记录并将其保存到数据库或检索会更容易。
  5. Would the subject_id in TEACHER have value when i get the data in Entity or it will be just a reference named subject,and i would not get that specific field in TEACHER.subject_id or @JoinColumn(name = "subject_id",referencedColumnName="sid") annotation will take care of that and it should have value in TEACHER.subject_id当我在 Entity 中获取数据时,TEACHER 中的 subject_id 是否有价值,或者它只是一个名为 subject 的引用,并且我不会在 TEACHER.subject_id 或@JoinColumn(name = "subject_id",referencedColumnName="sid")中获取该特定字段@JoinColumn(name = "subject_id",referencedColumnName="sid")注释会解决这个问题,它应该在 TEACHER.subject_id 中具有价值

Below are my answers and suggestions on your questions:以下是我对您的问题的回答和建议:

1.Which is ideal and correct out of the above two ways of writing a entity. 1.以上两种写实体的方式,哪一种是最理想和正确的。

Approach 2 is correct way of writing an enitiy.方法 2 是编写实体的正确方法。 Once you have defined @ManyToOne relation with another entity,no need to define columns specific to that entity.一旦您定义了与另一个实体的@ManyToOne关系,就无需定义特定于该实体的列。

2. From Hibernate and JPA perspective which is more efficient. 2. 从 Hibernate 和 JPA 的角度来看哪个更高效。

JPA is the specification for management of relational data in Java and Hibernate is the Object-Relational Mapping(ORM) tool that saves the state of objects into DB. JPA is the specification for management of relational data in Java and Hibernate is the Object-Relational Mapping(ORM) tool that saves the state of objects into DB. Simply put, Hibernate is the implementation of JPA.简单来说,Hibernate就是JPA的实现。 Hence, comparing them should not be a question.因此,比较它们不应该是一个问题。 You can compare Hibernate with other ORM tools that can be used with Spring framework like iBatis, EclipseLink,etc.您可以将 Hibernate 与其他可与 Spring 框架(如 iBatis、EclipseLink 等)一起使用的 ORM 工具进行比较。

3. Will there be any difference on the sql generated based on what approach i chose to write my entity. 3. 根据我选择编写实体的方法生成的 sql 会有什么不同吗?

No,generated SQL will be the same.不,生成的 SQL 将是相同的。 But, to generate that SQL, you can use different approaches like @NativeSqlQuery , Hibernate Query Language(HQL) ,etc.但是,要生成 SQL,您可以使用不同的方法,例如@NativeSqlQueryHibernate Query Language(HQL)等。

4.If i take 2nd approach will it be more easier to query and save records to DB or to retrieve. 4.如果我采用第二种方法,查询记录并将其保存到数据库或检索会更容易。

You should definitely use 2nd approach for clear,staright-forward and relational approach.您绝对应该使用第二种方法来实现清晰、直接和相关的方法。 1st approach is wrong to its core if you take about the principles of object-oriented language and relational database management.如果您考虑面向对象语言和关系数据库管理的原则,则第一种方法的核心是错误的。 No need to create extra columns,code,entity properties, and other mistakes when you can easily link them through mapping.当您可以通过映射轻松链接它们时,无需创建额外的列、代码、实体属性和其他错误。

5.Would the subject_id in TEACHER have value when i get the data in Entity or it will be just a reference named subject,and i would not get that specific field in TEACHER.subject_id or @JoinColumn(name = "subject_id",referencedColumnName="sid") annotation will take care of that and it should have value in TEACHER.subject_id 5.当我在实体中获取数据时,TEACHER 中的 subject_id 是否有价值,或者它只是一个名为 subject 的引用,我不会在 TEACHER.subject_id 或 @JoinColumn(name = "subject_id",referencedColumnName= 中获取该特定字段"sid") 注释会解决这个问题,它应该在 TEACHER.subject_id 中具有价值

In your DB,you should have a column subject_id in your Teacher table that will be a foreign key and references sid column of Subject table.在您的数据库中,您的Teacher表中应该有一个subject_id列,该列将作为外键并引用Subject表的sid列。 This happens at DB end.这发生在数据库端。
At your Object-oriented end (Spring), you can use create object reference for Subject to map with Student or you can directly use field subject_id in your HQL or NativeQuery to perform DB operations and persist the data.在您的面向对象端(Spring),您可以使用创建 object 参考为Subject to map with Student ,或者您可以直接使用HQL 或 NativeQuery中的字段subject_id来执行数据库操作并保存数据。 Hence, you will get subject_id in your Teacher entity.因此,您将在Teacher实体中获得subject_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM