如何解决Java JPA使用其他列在哪里从其他表中选择列
我有两个表:
create table table_binding (id bigint not null,description varchar(255),primary key (id));
和
create table table2 (id bigint not null,value clob,type varchar(255));
例如,表格已填充下一个值:
table_binding:
|---------------------|------------------|
| ID | Description |
|---------------------|------------------|
| 1 | Some Desc |
|---------------------|------------------|
| 2 | Other Desc |
|---------------------|------------------|
表2:
|---------------------|------------------|------------------|
| ID | value | type |
|---------------------|------------------|------------------|
| 1 | VALUE_A | TYPE1 |
|---------------------|------------------|------------------|
| 1 | VALUE_B | TYPE2 |
|---------------------|------------------|------------------|
| 2 | VALUE_C | TYPE1 |
|---------------------|------------------|------------------|
| 2 | VALUE_D | TYPE2 |
|---------------------|------------------|------------------|
我只有一个实体
@Entity(name = "table_binding")
@Data
public class myBinding(){
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE)
Long id;
private String description;
@Clob
private String value1;
@Clob
private String value2;
}
我如何修改实体类代码以使value1和value2与下一个sql查询的返回值相同: 值1:
select value.t2 from table2 t2,table_binding t1 where t2.type="TYPE1" and t2.id=t1.id and t1.id=?
value2:
select value.t2 from table2 t2,table_binding t1 where t2.type="TYPE2" and t2.id=t1.id and t1.id=?
我的意思是id = 1的myBinding实体将是: id = 1; descripton =“ Some Desc”; valu1 =“ VALUE_A”; value2 =“ VALUE_B”
我知道@SecondaryTable和@Column批注,但是它们不允许实现
where t2.type="TYPE1"
解决方法
实体必须是数据库的忠实代表。
根据您选择的配置,由于您在实体中指示TABLE_BINDING表没有列VALUE1和VALUE2,因此该应用程序不会取消。
您能做的最好的事情是为两个实体建模,一个实体用于每个具有各自列的表,并实现一个查询,该查询返回所需的数据,或者在两者之间使用fk或使用子查询,或者将两个表添加到从。以及where子句的必要条件,以避免使用笛卡尔积,而不接触实体。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。