使用hibernate插入多对多关系时出错

时间:2014-04-24 09:39:57

标签: java spring hibernate

我试图使用hibernate插入多对多的关系,但是我收到了这个错误。

  

2014-04-24 14:50:47,820 ERROR [BasicPropertyAccessor.java:118]:类中的IllegalArgumentException:com.jellboi.maniartyre.entities.AbstractEntity,属性的setter方法:pkey

     

2014-04-24 14:50:47,827 ERROR [BasicPropertyAccessor.java:122]:期望类型:java.lang.Long,实际值:org.hibernate.id.IdentifierGeneratorHelper $ 2

     

2014年4月24日下午2:55:25 org.apache.catalina.core.StandardWrapperValve调用   严重:servlet applicationController的Servlet.service()引发了异常   java.lang.IllegalArgumentException:java.lang.ClassCastException@17d66f6       at sun.reflect.GeneratedMethodAccessor27.invoke(Unknown Source)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)       在java.lang.reflect.Method.invoke(Method.java:597)

这是我正在尝试的代码。 VehicleProduct类

@Entity
@Table(name="m_vehicle_product")
@AssociationOverrides({
@AssociationOverride(name = "pk.vehicle",
        joinColumns = @JoinColumn(name = "vehicle_id")),
@AssociationOverride(name = "pk.product",
        joinColumns = @JoinColumn(name = "product_id")),
 })
public class VehicleProduct extends AbstractEntity{

private String service;
private VehicleProductId pk = new VehicleProductId();

@Column(name = "service")
public String getService() {
    return service;
}

public void setService(String service) {
    this.service = service;
}

@EmbeddedId
public VehicleProductId getPk() {
    return pk;
}

public void setPk(VehicleProductId pk) {
    this.pk = pk;
}


@Transient
public Product getProduct(){
    return getPk().getProduct();
}

public void setProduct(Product product){
    getPk().setProduct(product);
}

@Transient
public Vehicle getVehicle(){
    return getPk().getVehicle();
}

public void setVehicle(Vehicle vehicle){
    getPk().setVehicle(vehicle);
}   
}

VehicleProductId Class

@Embeddable
public class VehicleProductId implements java.io.Serializable {

private Vehicle vehicle;
private Product product;

@ManyToOne
public Vehicle getVehicle() {
    return vehicle;
}

public void setVehicle(Vehicle vehicle) {
    this.vehicle = vehicle;
}

@ManyToOne
public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}   
}

这就是我插入的方式。

for(int i=0;i<jobid.length;i++){
            product  = productService.findByPkey(jobid[i]);
            vehicleProduct.setProduct(product);
            vehicleProduct.setService(jobdesc[i]);  
            pkey2 = vehicleProductService.save(vehicleProduct);
}

请指导我。试着解决这个问题。

修改

@MappedSuperclass
public class AbstractEntity implements IEntity, Serializable{

private static final long serialVersionUID = 1L;

private Long pkey;
private Boolean deleted;
private String creator;
private Date created;
private String changer;
private Date changed; 
private Long version;


@Id
@GeneratedValue
@Column(name="pkey")
public Long getPkey() {
    return pkey;
}
public void setPkey(Long pkey) {
    this.pkey = pkey;
}

@Column(name="deleted")
@XmlTransient
public Boolean getDeleted() {
    return deleted;
}
public void setDeleted(Boolean deleted) {
    this.deleted = deleted;
}

@Column(name="creator")
public String getCreator() {
    return creator;
}
}........

它包含所有这些getter和setter。

2 个答案:

答案 0 :(得分:0)

2014-04-24 14:50:47,820 ERROR [BasicPropertyAccessor.java:118]:类中的IllegalArgumentException:com.jellboi.maniartyre.entities.AbstractEntity,属性的setter方法:pkey

2014-04-24 14:50:47,827 ERROR [BasicPropertyAccessor.java:122]:期望类型:java.lang.Long,实际值: org.hibernate.id.IdentifierGeneratorHelper $ 2 < / p>

我不知道这是你的情况,但看看你的踪迹我不得不说hibernate不支持具有身份部分的复合PK

Hibernate Jira composite PK identity part

答案 1 :(得分:0)

你的主要问题是:

  

2014-04-24 14:50:47,820 ERROR [BasicPropertyAccessor.java:118]:类中的IllegalArgumentException:com.jellboi.maniartyre.entities.AbstractEntity,属性的setter方法:pkey

     

2014-04-24 14:50:47,827 ERROR [BasicPropertyAccessor.java:122]:期望类型:java.lang.Long,实际值:org.hibernate.id.IdentifierGeneratorHelper $ 2

如果您查看代码,您的AbstractEntity上定义了@Id,而您的VehicleProduct上定义了@EmbeddedId

我不确定您的数据库表应该如何显示,但它似乎包含AbstractEntity中的列以及VehicleProduct中定义的列。如果列不在那里,那么你不应该继承AbstractEntity。如果它们应该在那里,那么请考虑将@EmbeddedId变为@Embedded并对业务键强制执行唯一约束。