java - JPA 多对一在引用表中具有常量值

标签 java mysql hibernate jpa spring-boot

我正在做 Spring Boot 项目并使用 spring-boot-jpa(Hibernate 实现)。我在配置实体之间的以下关系时遇到问题。

假设我需要两个表之间的多对一(以及相反的一对多)关系(本例中的 MySQL,table1 在逻辑上存储各种其他表中代码的描述):

CREATE TABLE `table1` (
   `id` INT NOT NULL AUTO_INCREMENT,
   `ref_table` VARCHAR(50) NOT NULL,
   `ref_column` VARCHAR(50) NOT NULL,
   `code` VARCHAR(10) NOT NULL,
   `description` VARCHAR(100) NOT NULL,
   PRIMARY KEY (`id`),
   UNIQUE INDEX `u_composite1` (`ref_table` ASC, `ref_column` ASC, `code` ASC));

CREATE TABLE `table2` (
   `id` INT NOT NULL AUTO_INCREMENT,
   `field1` VARCHAR(100) NULL,
   `code` VARCHAR(10) NOT NULL,
   PRIMARY KEY (`id`));

我在SQL中连接这两个表的方式是这样的:

SELECT t2.*, t1.description 
FROM table2 t2 
JOIN table1 t1 
  ON    ( t1.ref_table = 'table2'
      AND t1.ref_column = 'code' 
      AND t1.code = t2.code);

所以,我创建了这样的实体(减去了 getter 和 setter):

@Entity
@Table(name = "table1")
public class Table1  implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false)
    private int id;

    @Column(nullable = false, length = 10)
    private String code;

    @Column(length = 100)
    private String description;

    @Column(name = "ref_column", nullable = false, length = 50)
    private String refColumn;

    @Column(name = "ref_table", nullable = false, length = 50)
    private String refTable;

    @OneToMany(mappedBy = "table1")
    private List<Table2> table2;
}

@Entity
@Table(name = "table2")
public class Table2  implements Serializable {
    private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(unique = true, nullable = false)
   private int id;

   @Column(nullable = false, length = 45)
   private String field1;

   @ManyToOne(fetch=FetchType.LAZY)
   @Column(name = "code")
   @JoinColumns({
      @JoinColumn(name = "code", referencedColumnName = "code", nullable = false, updatable = false),
      @JoinColumn(name = "'table2'", referencedColumnName = "ref_table", nullable = false, updatable = false),
      @JoinColumn(name = "'code'", referencedColumnName = "ref_column", nullable = false, updatable = false)
   })
   private Table1 table1;

}

但它不起作用。 :(

这种关系甚至可以在 JPA 中定义吗? 如果是,请问如何?

最佳答案

关于“使用常量值连接”问题,我设法使用@Where Hibernate 注释使其工作:

  • How to replace a @JoinColumn with a hardcoded value?

    @Entity
    @Table(name = "a")
    public class A {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        public long id;
    
        @OneToMany
        @JoinColumn(name = "id", referencedColumnName = "id")
        @Where(clause = "blah = 'CONSTANT_VALUE'")
        public Set<B> b;
    
        protected A() {}
    }
    
    @Entity
    @Table(name = "b")
    public class B {
        @Id
        @Column(nullable = false)
        public Long id;
    
        @Column(nullable = false)
        public String blah;
    
        protected B() {}
    }
    

关于java - JPA 多对一在引用表中具有常量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30571450/

相关文章:

database - 使用 Hibernate 和 Spring 根据用户登录连接到租户数据库

java - 一个线程比另一个慢,即使他们做同样的事情

java - 使用动态键序列化 JSON 响应

java - 将 InputStream 对象转换为 String 的最佳方法

mysql - 选择语句查询以排除具有重复值的行

如果表中不存在,MySQL 选择一个字段为 NULL

java - @CollectionTable 和 @ElementCollection 正在生成异常

java - 如何修复空指针异常

mysql - 在rails或sql中检索开始日期和结束日期之间的日期在今天和十天之后的数据

java - Hibernate C3P0 池机制