Java JPA引用表中具有常量值的多对一
我正在做SpringBoot项目,并使用SpringBootJPA(Hibernate实现)。我在配置实体之间的以下关系时遇到问题 让我们假设我需要两个表之间的多对一(反过来是一对多)关系(在本例中,MySQL,Java JPA引用表中具有常量值的多对一,java,mysql,hibernate,jpa,spring-boot,Java,Mysql,Hibernate,Jpa,Spring Boot,我正在做SpringBoot项目,并使用SpringBootJPA(Hibernate实现)。我在配置实体之间的以下关系时遇到问题 让我们假设我需要两个表之间的多对一(反过来是一对多)关系(在本例中,MySQL,table1逻辑上存储其他各种表中代码的描述): 我在SQL中连接这两个表的方式如下: SELECT t2.*, t1.description FROM table2 t2 JOIN table1 t1 ON ( t1.ref_table = 'table2'
table1
逻辑上存储其他各种表中代码的描述):
我在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):
@实体
@表(name=“表1”)
公共类Table1实现了可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(unique=true,nullable=false)
私有int-id;
@列(nullable=false,长度=10)
私有字符串码;
@列(长度=100)
私有字符串描述;
@列(name=“ref_Column”,null=false,长度=50)
私有字符串列;
@列(name=“ref_table”,null=false,长度=50)
私有字符串引用表;
@OneToMany(mappedBy=“表1”)
私人名单表2;
}
@实体
@表(name=“表2”)
公共类Table2实现了可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(unique=true,nullable=false)
私有int-id;
@列(nullable=false,长度=45)
私有字符串字段1;
@manytone(fetch=FetchType.LAZY)
@列(name=“code”)
@连接柱({
@JoinColumn(name=“code”,referencedColumnName=“code”,nullable=false,updateable=false),
@JoinColumn(name=“'table2”,referencedColumnName=“ref_table”,nullable=false,updateable=false),
@JoinColumn(name=“'code”,referencedColumnName=“ref\u column”,nullable=false,updateable=false)
})
私人附表1附表1;
}
但它不起作用(
这种关系甚至可以在JPA中定义吗?
如果是这样的话,请告诉我如何处理?关于“使用常量值连接”问题,我使用@Where Hibernate注释成功地解决了这个问题:
-
@实体 @表(name=“a”) 公共A类{ @身份证 @GeneratedValue(策略=GenerationType.AUTO) 公共长id; @独身癖 @JoinColumn(name=“id”,referencedColumnName=“id”) @其中(clause=“blah=”常量值“) 公共集合b; 受保护的A(){} } @实体 @表(name=“b”) 公共B级{ @身份证 @列(nullable=false) 公共长id; @列(nullable=false) 公共字符串废话; 受保护的B(){} }
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);
@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;
}
@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() {}
}