Annotation to join columns with OR condition instead of AND condition

前端 未结 2 460
一个人的身影
一个人的身影 2020-12-20 18:25

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

Person:

@Entity
@Table(name = \"persons\         


        
相关标签:
2条回答
  • 2020-12-20 18:52

    You can use @FilterDef and @Filter annotations.

    0 讨论(0)
  • 2020-12-20 19:03

    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.

    0 讨论(0)
提交回复
热议问题