简体   繁体   中英

Error while Inserting into many to many relationship using hibernate

I am trying to insert into many to many relationship using hibernate but I am getting this error.

2014-04-24 14:50:47,820 ERROR [BasicPropertyAccessor.java:118] : IllegalArgumentException in class: com.jellboi.maniartyre.entities.AbstractEntity, setter method of property: pkey

2014-04-24 14:50:47,827 ERROR [BasicPropertyAccessor.java:122] : expected type: java.lang.Long, actual value: org.hibernate.id.IdentifierGeneratorHelper$2

Apr 24, 2014 2:55:25 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet applicationController threw exception java.lang.IllegalArgumentException: java.lang.ClassCastException@17d66f6 at sun.reflect.GeneratedMethodAccessor27.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597)

Here is the code that I am trying. VehicleProduct class

@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;
}   
}

And this is how I am Inserting.

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

Please guide me on this. Trying since hours to solve this problem.

EDIT

@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;
}
}........

It contains all of these getter and setters.

2014-04-24 14:50:47,820 ERROR [BasicPropertyAccessor.java:118] : IllegalArgumentException in class: com.jellboi.maniartyre.entities.AbstractEntity, setter method of property: pkey

2014-04-24 14:50:47,827 ERROR [BasicPropertyAccessor.java:122] : expected type: java.lang.Long, actual value: org.hibernate.id.IdentifierGeneratorHelper$2

I do not know it this is your case, but taking a look to your trace I have to say hibernate does not support composite PK's with an identity part

Hibernate Jira composite PK identity part

Your main problem is this:

2014-04-24 14:50:47,820 ERROR [BasicPropertyAccessor.java:118] : IllegalArgumentException in class: com.jellboi.maniartyre.entities.AbstractEntity, setter method of property: pkey

2014-04-24 14:50:47,827 ERROR [BasicPropertyAccessor.java:122] : expected type: java.lang.Long, actual value: org.hibernate.id.IdentifierGeneratorHelper$2

If you look at your code, you have an @Id defined on your AbstractEntity and an @EmbeddedId on your VehicleProduct

I am not sure how your database table is supposed to look, but it will seem to include the columns in AbstractEntity as well as those defined in VehicleProduct. If the columns are not meant to be there, then you shouldn't inherit from AbstractEntity. If they were meant to be there, then consider making the @EmbeddedId into an @Embedded and enforce a unique constraint for the business key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM