java调用com主键_java – 使用复合主键的SELECT查询

在使用hibernate和jpa的spring mvc app中,我最近使用@Embeddable类切换到复合主键.因此,我需要更新基于其唯一ID返回给定对象的JPA查询.以下是过去工作的JPA代码,但不再返回结果:

@SuppressWarnings("unchecked")

public Concept findConceptById(BigInteger id) {

Query query = this.em.createQuery("SELECT conc FROM Concept conc WHERE conc.id =:cid");

query.setParameter("cid", id);

return (Concept) query.getSingleResult();

}

如何更改上面的查询,以便返回具有给定id的最新有效时间的Concept?请注意,id和effectiveTime是ConceptPK复合主键的两个属性,因此id和effectiveTime的属性定义和getter以及setter在ConceptPK类中,而不在Concept类中.

上面抛出的错误是:

Caused by: java.lang.IllegalArgumentException:

Parameter value [786787679] did not match expected type [myapp.ConceptPK]

这是现在在Concept类中定义主键的方式:

private ConceptPK conceptPK;

这是ConceptPK类的代码:

@Embeddable

class ConceptPK implements Serializable {

@Column(name="id", nullable=false)

protected BigInteger id;

@Column(name="effectiveTime", nullable=false)

@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")

private DateTime effectiveTime;

public ConceptPK() {}

public ConceptPK(BigInteger bint, DateTime dt) {

this.id = bint;

this.effectiveTime = dt;

}

/** getters and setters **/

public DateTime getEffectiveTime(){return effectiveTime;}

public void setEffectiveTime(DateTime ad){effectiveTime=ad;}

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

public BigInteger getId() {return id;}

@Override

public boolean equals(Object obj) {

if (this == obj) return true;

if (obj == null) return false;

if (getClass() != obj.getClass()) return false;

final ConceptPK other = (ConceptPK) obj;

if (effectiveTime == null) {

if (other.effectiveTime != null) return false;

} else if (!effectiveTime.equals(other.effectiveTime)) return false;

if (id == null) {

if (other.id != null) return false;

} else if (!id.equals(other.id)) return false;

return true;

}

@Override

public int hashCode() {

int hash = 3;

hash = 53 * hash + ((effectiveTime == null) ? 0 : effectiveTime.hashCode());

hash = 53 * hash + ((id == null) ? 0 : id.hashCode());

return hash;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值