使用OR条件而不是AND条件连接列的注释 [英] Annotation to join columns with OR condition instead of AND condition

查看:198
本文介绍了使用OR条件而不是AND条件连接列的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个java类, Relation Person ,它们都存在于我的数据库中。



Person:

  @Entity 
@Table(name = person)
public class Person {
@Id
@Column
private int id;

@Column
私人字符串名称;

@OneToMany(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name =slave_id,referencedColumnName =id),
@ JoinColumn(name =master_id,referencedColumnName =id)
})
private List< Relation>关系;

// Getters and setters
}

关系:

  @Entity 
@Table(name =relations)
公共类关系{
@Id
@Column
private int id;

@Column
private int child_id;

@Column
private int parent_id;

@Column
私有字符串类型;

// Getters and setters
}

每个人都有关系列表(或不),当关系的child_id或parent_id等于人的id时,关系应该被添加到列表中。



TL; DR:
relation.child_id或relation.parent_id = person.id =>将关系添加到人员关系列表中时

$ b

我遇到的问题是这个注解:

  @JoinColumns({
@JoinColumn(name =child_id,referencedColumnName =id),
@JoinColumn(name =parent_id,referencedColumnName =id)
})

$ b

创建以下SQL(只是必要的部分):

 关系关系6_ 
在this_.id = relations6_.slave_id
和this_.id = relations6_.master_id

Java Hibernate中ge正确的注释是什么?使用 OR 而不是 AND

解释方法

您可以使用的选项:


  1. 数据库视图。为您创建自定义连接并将实体映射到视图的视图。 加入公式。我设法让他们只在多对一的协会工作。不过,您可以将关联设为双向并将公式应用于 Relation 实体中。

  2. @Subselect 。这是一种Hibernate视图,适用于无法创建实际数据库视图或更改数据库模式以更好地适应实体模型结构的情况。



另外,您总是可以为奴隶和主人使用两个单独的关联:

  public class Person {
@OneToMany
@JoinColumn(name =slave_id),
private List< Relation>奴隶;

@OneToMany
@JoinColumn(name =master_id),
private List< Relation>主人;

公共列表<关系> getRelations(){
List< Relation> result = new ArrayList<>(slaves);
result.addAll(masters);
返回结果;


$ / code>

然而,请记住,加入所有这些一个查询需要主从机之间的完整笛卡尔积。


I have 2 java classes, Relation and Person, which both are present in my database.

Person:

@Entity
@Table(name = "persons")
public class Person {
    @Id
    @Column
    private int id;

    @Column
    private String name;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumns({
        @JoinColumn(name = "slave_id", referencedColumnName="id"),
        @JoinColumn(name = "master_id", referencedColumnName="id")
    })
    private List<Relation> relations;

    //Getters and setters
}

Relation:

@Entity
@Table(name = "relations")
public class Relation {
    @Id
    @Column
    private int id;

    @Column
    private int child_id;

    @Column
    private int parent_id;

    @Column
    private String type;

    //Getters and setters
}

Each Person has a list of relations (or not), the relation should be added to the list when the child_id or the parent_id of the relation is equal to the id of the person.

TL;DR: When relation.child_id OR relation.parent_id = person.id => add relation to list of relations to the person

The issue I am facing is that this annotation:

@JoinColumns({
            @JoinColumn(name = "child_id", referencedColumnName="id"),
            @JoinColumn(name = "parent_id", referencedColumnName="id")
        })

creates following SQL (just the necessary part):

relations relations6_ 
            on this_.id=relations6_.slave_id 
            and this_.id=relations6_.master_id

What is the correct annotation in Java Hibernate to generate an SQL statement saying OR instead of AND

解决方案

Some of the options that you could utilize:

  1. Database views. Create the view that does custom join for you and map the entity to the view.
  2. Join formula. I managed to make them work only on many-to-one associations. Nevertheless, you could make the association bidirectional and apply the formula in the Relation entity.
  3. @Subselect. This is a kind of Hibernate view, suitable if you can't afford to create a real database view or change the db schema to better suit the entity model structure.

This and this answer could also be helpful.

Also, you can always use two separate associations for slaves and masters:

public class Person {
    @OneToMany
    @JoinColumn(name = "slave_id"),
    private List<Relation> slaves;

    @OneToMany
    @JoinColumn(name = "master_id"),
    private List<Relation> masters;

    public List<Relation> getRelations() {
        List<Relation> result = new ArrayList<>(slaves);
        result.addAll(masters);
        return result;
    }
}

However, keep in mind that joining all of them in a single query requires full Cartesian product between masters and slaves.

这篇关于使用OR条件而不是AND条件连接列的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆