英文:
Spring Jpa entity convention of linking to other entities
问题
I see some different implementations of linking one entity to others. For example, most JPA with Hibernate use relations annotations, for example:
@Entity
@Data
public class Employee {
//id
.....
@JoinTable(...)
@ManyToMany
Set<Company> companies;
}
@Entity
@Data
public class Company {
//id
.....
@JoinTable(...)
@ManyToMany
Set<Employee> employees;
}
But in some occasions, this is used:
@Entity
@Data
public class Employee {
//id
.....
private Long companyId;
}
@Entity
@Data
public class Company {
//id
private Long employeeId;
}
And then use @Query(value="SELECT ... FROM ... WHERE employeeId / companyId =: ....", nativeQuery = true)
in respective repository interface extending JpaRepository<....> to select employees/companies with particular fk.
Which one is preferred? I know lots of tutorials use the first one, but I think the second seems to be easier? That being said, is the second one even accepted in JPA annotation? Should I stick with the first annotation?
英文:
I see some different implementations of linking one entity to others. For example, most Jpa with hibernate use relations annotations, for example:
@Entity
@Data
public class Employee {
//id
.....
@JoinTable(...)
@ManyToMany
Set<Company> companies;
}
@Entity
@Data
public class Company {
//id
.....
@JoinTable(...)
@ManyToMany
Set<Employee> employees;
}
But in some occasions, this is used:
@Entity
@Data
public class Employee {
//id
.....
private Long companyId;
}
@Entity
@Data
public class Company {
//id
private Long employeeId;
}
And then use @Query(value="SELECT ... FROM ... WHERE employeeId / companyId =: ....", nativeQuery = true)
in respective repository interface extending JpaRepository<....> to select employees/companies with particular fk. <br>
which one is preferred? I know lots of tutorials use the first one, but I think the second seems to be easier? That being said, is the second one even accepted in jpa annotation? Should I stick with the first annotation?
答案1
得分: 1
以下是翻译的内容:
在计算机科学中,没有适用于所有情况的“最佳方法”,它非常依赖于每个项目的具体细节。
首先,重要的是要注意,您的项目不反映真正的多对多关系,一个公司可以拥有多名员工,但员工是否能同时在多家公司工作并没有太多现实意义。
当定义这种类型的关系时,我特别使用@ManyToMany
,尽量避免使用@JoinTable
,因为这完全不必要,除非您想要强制指定联接表或其列的特定名称,Hibernate默认会为它们命名。
我从未见过有人像您在第二个示例中定义数据模型,它肯定无法表示多对多关系。
如果在员工实体中放置company_id
,那么您如何表示员工拥有多家公司?这在数学上根本不可能。
此外,通过使用Long
来设置关联关系中的外键,世界上没有任何ORM工具能够解析和为您创建实体,您需要自行处理所有外键、解析和映射您的实体,因为Hibernate无法猜测到:
private Long employeeId;
表示一个反映到第二个表的id的外键。
最后但同样重要的是,除非绝对必要,严格不建议使用@Query("anything", nativeQuery = true)
。通过将查询定义为本地查询,您基本上破坏了JPA的DBMS之间互操作性的目的。
英文:
There is no "best approach for all cases" when it comes to computer science, it depends very much on specific details of each project.
First of all it is important to note that your project doesn't reflect a real Many To Many relationship, one company has many employees, but can an employee bet at multiple companies at same time? makes not much sense in real world.
I particularly use the @ManyToMany
when it comes to defining this kind of relationships... I avoid at maximum using @JoinTable
as it is not necessary at all, you just need to use this annotation in case you want to force a specific name for the join table or its columns, by default hibernate will name them.
I've never seen anyone defining the data model as you made in the second example and it for sure wont be able to represent a many to many relationship
if you put the company_id in the employee entity how do you want to represent the employee having multiple companies? it wont be mathematically possible at all
Also, by using a long to set the foreigner key in your relationship no ORM tool in the world will be able to parse and create the entities for you, you gonna yourself need to take care of all foreigner keys, parsing and mapping of your entities since Hibernate has no way to guess that:
private Long employeeId;
represents a foreigner key that comes to reflect an id in a second table
last but not least, it is strictly not recommendable to use @Query("anything", nativeQuery = true)
unless strictly necessary. By defining your queries as native you basically killing the interoperability of DBMS that is the purpose of JPA
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论