如何将OneToMany和ManyToOne与两个由JPA中的命名查询生成的实体一起使用

时间:2015-04-15 15:25:16

标签: java hibernate jpa entity

我有这两个条款:

package cu.jpa.entities.views;

import cu.jpa.entities.BaseEntity;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import java.util.List;

@Entity
@NamedQuery(
        name="SubstanceDescriptionEntity.findAll",
        query="select " +
                "    new cu.jpa.entities.views.SubstanceDescriptionEntity(S.id, S.substanceId) " +
                "from " +
                "    SubstanceEntity S "
)
public class SubstanceDescriptionEntity extends BaseEntity {

    private int id;
    private String substanceId;
    private List<PatternDescriptionEntity> patterns;

    public SubstanceDescriptionEntity(int id, String substanceId){
        this.id = id;
        this.substanceId = substanceId;
    }
    public SubstanceDescriptionEntity(){

    }


    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSubstanceId() {
        return substanceId;
    }

    public void setSubstanceId(String substanceId) {
        this.substanceId = substanceId;
    }

    @OneToMany(mappedBy = "substance")
    public List<PatternDescriptionEntity> getPatterns() {
        return patterns;
    }

    public void setPatterns(List<PatternDescriptionEntity> patterns) {
        this.patterns = patterns;
    }
}

这是第2课:

package cu.jpa.entities.views;

import cu.jpa.entities.BaseEntity;

import javax.persistence.*;

@Entity
@NamedQuery(
        name="PatternDescriptionEntity.findAll",
        query="select " +
                "    new cu.jpa.entities.views.PatternDescriptionEntity(P.id, " +
                "    E.encounterId, " +
                "    P.patternId, " +
                "    E.substanceId) " +
                "from " +
                "    PatternEntity P " +
                "    inner join " +
                "    P.encounterByEncounterId E " +
                "    where P.encounterId = E.id "
)
public class PatternDescriptionEntity extends BaseEntity {
    private int id;
    private String patternId;
    private String encounterId;
    private int substanceId;

    private SubstanceDescriptionEntity substance;

    public PatternDescriptionEntity(int id, String encounterId, String patternId,  int substanceId){
        this.id = id;
        this.patternId = patternId;
        this.encounterId = encounterId;
        this.substanceId = substanceId;
    }
    public PatternDescriptionEntity(){

    }


    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPatternId() {
        return patternId;
    }

    public void setPatternId(String patternId) {
        this.patternId = patternId;
    }

    public String getEncounterId() {
        return encounterId;
    }

    public void setEncounterId(String encounterId) {
        this.encounterId = encounterId;
    }

    public int getSubstanceId() {
        return substanceId;
    }

    public void setSubstanceId(int substanceId) {
        this.substanceId = substanceId;
    }

    @ManyToOne
    @JoinColumn(name = "substanceId", insertable = false, updatable = false, referencedColumnName = "substanceId")
    public SubstanceDescriptionEntity getSubstance() {
        return substance;
    }

    public void setSubstance(SubstanceDescriptionEntity substance) {
        this.substance = substance;
    }
}

这两个类都是命名查询执行的结果。我可以使用这个正确地获取所有元素:

@Override
public List<PatternDescriptionEntity> findAll(){
    Query query = entityManager.createNamedQuery("PatternDescriptionEntity.findAll",PatternDescriptionEntity.class);
    List<PatternDescriptionEntity> items =  query.getResultList();
    return items;
}

问题在于我想获得对应于 PatternDescriptionEntity SubstanceDescriptionEntity 的引用,根据我认为我应该放在@ManyToOne中的详细信息注解。两个实体都是命名查询的结果,而不是表映射的结果。这就是我遇到麻烦的原因。因为当我执行这个测试时:

@Autowired
PatternDescriptionService service;

@Test
public void test_count_with_filters() throws Exception {
    List<PatternDescriptionEntity> patterns = service.findAll();
}

我可以看到带有值的模式列表,但列表中的每个单独实例都具有null属性实体,这意味着映射存在某种麻烦,我可以理解它。 所以问题是: 我有这两个JPA实体(没有映射到数据库表,但是到命名查询结果)。如何定义ManyToOne和OneToMany的关系?

0 个答案:

没有答案