复合主键的一部分也是外键

时间:2014-06-11 10:19:51

标签: java hibernate jpa hql composite-primary-key

我有两个表,一个有两部分复合主键,第一部分也是另一个表的主键。

CustomerOfferAcceptance
offer_id // primary key part 1 also a fk
customer_id // primary key part 2

优惠
id // primary key

我希望能够在offer_id和id。

上加入这两个表

我的代码

@Entity
@Table(name = "customer_offer_acceptance", schema = "rz_customer")
public class CustomerOfferAcceptance {

    @EmbeddedId
    private CustomerOfferPK key;

    @MapsId("offerId")
    @ManyToOne
    @JoinColumn(name="offer_id", referencedColumnName = "id")
    private Offer offer;
}

@Embeddable
public class CustomerOfferPK implements Serializable {

    private static final long serialVersionUID = -8847738316352487377L;

    @Column(name = "customer_id")
    private String customerId;

    @Column(name = "offer_id")
    private String offerId;

    public CustomerOfferPK() { }

    public CustomerOfferPK(String customerId, String offerId) {
    super();
    this.customerId = customerId;
    this.offerId = offerId;
    }
}

@Entity
@Table(name = "offer", schema = "rz_customer")
public class Offer implements Serializable{

    private static final long serialVersionUID = -7842210981812803506L;

    @Id
    @Column(name="id")
    private String id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "offer")
    private Collection<CustomerOfferAcceptance> CustomerOfferAcceptance;
}  

解析此查询时出现异常

从persistence.domain.CustomerOfferAcceptance中选择o coa inner join persistence.domain.Offer o其中coa.customer_id =:someId

例外:

Caused by: java.lang.IllegalStateException: DOT node with no left-hand-side!
    at org.hibernate.hql.internal.ast.tree.DotNode.getLhs(DotNode.java:667)
    at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:644)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:389)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3858)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3644)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3522)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)

非常感谢!

1 个答案:

答案 0 :(得分:0)

问题出在查询中。这个没有例外

从persistence.domain.Offer o,persistence.domain.CustomerOfferAcceptance coa中选择o,其中o = coa.offer和coa.key.customerId =:id