JPA如何选择没有多个属性的对象

时间:2012-10-09 07:58:47

标签: hibernate jpa

我使用hibernate 3.5.4-final。我有两个JPA课程。

    @Entity
    @Table(name = "SPI_V_OP_OBJ_SUBJECT_VR")
    public class OpSubject_vr extends PersistenceEntity implements Serializable {

        private static final long serialVersionUID = 1858749317461707015L;
        /** id */
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "ID",
                nullable = false)
        private Long id;

        /** name */
        @Column(name = "F_NAME")
        private String f_name;


        /** type */
        @Column(name = "SUBJECT_TYPE")
        private String subject_type;


        /** okved */
        @ManyToMany
        @JoinTable(name = "SPI_V_OP_OBJ_SUBJECT_VR_OKVED",
                   joinColumns = {@JoinColumn(name = "OBJECT_ID",
                                              referencedColumnName = "ID",
                                              nullable = false)},
                   inverseJoinColumns = {@JoinColumn(name = "SPR_ID",
                                                     referencedColumnName = "ID",
                                                     nullable = false)})
        private List<OpSprOkved> okved_id_mult = new ArrayList<>();
}

@Entity
@Table(name = "SPI_V_OP_SPR_OKVED")
public class OpSprOkved extends PersistenceEntity implements Serializable {

    /** id */
    @Id
    @Column(name = "ID", nullable = false)
    private Long id;

    /** code*/
    @Column(name = "CODE")
    private String code;

    /** name*/
    @Column(name = "name")
    private String name;
}

属性okved_id_mult是多个。我想选择具有空属性okved_id_mult的对象OpSubject_vr。

我试过

select ent from OpSubject_vr ent where okved_id_mult is empty; (hibernate error)

select ent from OpSubject_vr ent where okved_id_mult is null; (hibernate error)

select distinct ent from OpSubject_vr ent join ent.okved_id_mult i1 where i1.code is null;(return 0 objects)

2 个答案:

答案 0 :(得分:0)

  1. 查询没问题,但您需要将okved_id_mult视为ent.okved_id_mult。
  2. 限制集合属性是违法的。
  3. 您需要修改查询以使用左连接。然后它在逻辑上等同于第一个查询。
  4. 请参阅https://github.com/BatooOrg/BatooJPA/tree/master/community/src/test/java/org/batoo/jpa/community/test/t2

    上的测试用例

答案 1 :(得分:0)

使用“ not in”运算符。此查询将检查您的“ okved_id_mult.id”是否未出现在所有现有的OpSprOkved.id中:

Vector128<float> mf = Vector128.Create(42f);
Vector128<int> mi = mf.As<int, float>(); // acts as '_mm_castps_si128'