简体   繁体   English

如何spring jpa hibernate创建多对一但没有外键的实体

[英]how to spring jpa hibernate create Entity that has many to one but without foreign key

I got two tables that has the many to one relationship, but they do not have a foreign key.我有两个具有多对一关系的表,但它们没有外键。 Like the Student are many, and the Teacher is the one,就像Student很多,而Teacher是一个,

the entities for example:实体例如:

@Entity
@Table(name = "student")
class Student {


   @Column(name = "TeacherName")
   private String teacherName;

   @ManyToOne
   private Teacher teacher
}


@Entity
@Table(name = "teacher")
class Teacher {

 private String name;
}

When I query the students, the sql is :当我查询学生时,sql 是:

select * from Student as st INNER JOIN Teacher as tcr ON st.TeacherName = tcr.name;

I found the @ManyToOne can not work and it looks like it needs a foreign key.我发现@ManyToOne不能工作,看起来它需要一个外键。 The table can not provide such though.表不能提供这样虽然。

Can someone tell me how to configure the entity?有人能告诉我如何配置实体吗?

You would have to use the following mapping:您必须使用以下映射:

public class Teacher  {

    @OneToMany(mappedBy = "teacher")
    private Set<Student> students;

    @Column(name = "name")
    private String name;
}

public class Student {
    @ManyToOne
    @JoinColumn(name = "teacherName", referencedColumnName = "name")
    private Teacher teacher;

}

Then in HQL:然后在 HQL 中:

select s from Student s INNER JOIN s.teacher t where t.name = :name 

Alternatively If you want to stay with your current mapping and then you would need to use the 'old' style of join in hql to achieve joining by non-foreign-key columns:或者,如果您想保留当前的映射,那么您需要使用 hql 中的“旧”连接样式来实现非外键列的连接:

select s from Student s, Teacher t where t.name = s.teacherName and t.name = :name

暂无
暂无

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

相关问题 在休眠状态下没有外键的一对多 - one-to-many without foreign key in hibernate JPA 2 - 如何使用Spring Data JPA构建具有主键的实体,该主键也是外键? - JPA 2 - How to build entity that has Primary key that is also a foreign key using Spring Data JPA? 在spring boot JPA中,如何正确地将其实体表示具有外键关联的对象POST到另一个实体? - In spring boot JPA, how to properly POST an object whose entity representation has a foreign key association to a different entity? 如何在多对多关系中的一个操作中保存多个实体 [Spring Boot 2,JPA,Hibernate,PostgreSQL] - How to save multiple entity in one action related in Many to Many Relationship [Spring Boot 2, JPA, Hibernate, PostgreSQL] 如何创建一个外键和一个主键的复合键 Spring 数据 JPA - How to create a composite key of one foreign key and one primary key Spring Data JPA 一对多休眠:实体应包含外键还是整个对象 - Hibernate one to many: Should the entity contain the foreign key or the whole object Spring 启动 JPA 一对多双向外键为 null - Spring Boot JPA one-to-many bidirectional foreign key is null sql使用spring jpa更新一对多(外键为空) - sql update one to many using spring jpa (foreign key null) Spring JPA-多对一没有完整的异物 - Spring JPA - Many to one without full foreign object Hibernate / JPA自动创建外键而不是在多对一关联中使用现有外键 - Hibernate/JPA auto creates foreign key instead of using exisiting foreign key in many to one association
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM