There is a class Product:

@Entity @Table(name = "Products") public class Product { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @ManyToOne(targetEntity = Type.class) @JoinColumn(name = "type") private Set<Type> type = new HashSet<Type>(); @ManyToOne(targetEntity = Brand.class) @JoinColumn(name = "brand") private Set<Brand> brand = new HashSet<Brand>(); private String title; private long count; private long price; @OneToMany(cascade = CascadeType.ALL, mappedBy = "product_id") public long getId() { return id; } public void setId(long id) { this.id = id; } public Set<Type> getType() { return type; } public void setType(Set<Type> type) { this.type = type; } public Set<Brand> getBrand() { return brand; } public void setBrand(Set<Brand> brand) { this.brand = brand; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public long getCount() { return count; } public void setCount(long count) { this.count = count; } public long getPrice() { return price; } public void setPrice(long price) { this.price = price; } } 

And Brand class:

 @Entity @Table(name = "Brands") public class Brand { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String brand; @OneToMany(mappedBy = "Brands") public long getId() { return id; } public void setId(long id) { this.id = id; } public String getTitle() { return brand; } public void setTitle(String brand) { this.brand = brand; } @Override public String toString() { return "Brand {" + "id=" + id + ", brand=" + brand + '}'; } } 

It starts normally, but the error crashes:

 java.lang.IllegalArgumentException: Can not set java.util.Set field com.alpha.bean.Product.brand to com.alpha.bean.Brand 

I can not understand what the problem is

  • you about what dependences ask? where does the error occur? - Mikhail Vaysman

1 answer 1

Before using JPA refer to the documentation.

ManyToOne Defines a unique relationship with another class that has a plural plurality. Usually there is no need to specify the target object (targetEntity) , since it can usually be inferred from the type of the object being referenced

Example

  @ManyToOne(optional=false) @JoinColumn(name="CUST_ID", nullable=false, updatable=false) public Customer getCustomer() { return customer; } 

OneToMany Defines a multi-valued relationship with a one-to-many multiplicity.

Example

 @OneToMany(cascade=ALL, mappedBy="customer") public Set<Order> getOrders() { return orders; } 

getters / setters must match the name of the field, you have it does not match in the class Brand

Recreate heteras \ seters in the Brand class. Change the classroom antoatsii. In the Brand class, private String brand; field private String brand; and not like not getId();