4
votes

I create two classes with many to many relation between them, like as below:

@Entity
@Table(name = FoodEntity.TABLE_NAME)
public class FoodEntity extends BaseEntity<Long> {

public static final String TABLE_NAME = "T_FOOD";

@ManyToMany
@JoinTable(
    name = "T_FOOD_FOODCATEGORY",
    joinColumns =       { @JoinColumn(name = "FOOD_ID") }, 
    inverseJoinColumns =    { @JoinColumn(name = "FOOD_CATEGORY_ID") })
private Set<FoodCategoryEntity> categories;

public Set<FoodCategoryEntity> getCategories() {
return categories;
}

public void setCategories(Set<FoodCategoryEntity> categories) {
this.categories = categories;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "FOOD_ID", nullable = false)
@Override
public Long getId() {
return id;
}

@Override
public void setId(Long id) {
this.id = id;
}

}

and :

@Entity
@Table(name = FoodCategoryEntity.TABLE_NAME)
public class FoodCategoryEntity extends BaseEntity<Long> {

public static final String TABLE_NAME = "T_FOOD_CATEGORY";

@ManyToMany(mappedBy = "categories")
private Set<FoodEntity> foods;

public Set<FoodEntity> getFoods() {
return foods;
}

public void setFoods(Set<FoodEntity> foods) {
this.foods = foods;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "FOOD_CATEGORY_ID", nullable = false)
@Override
public Long getId() {
return id;
}

@Override
public void setId(Long id) {
this.id = id;
}

}

but when I test this relation with junit,spring and hibernate, I get below exception:

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: T_FOOD, for columns: [org.hibernate.mapping.Column(categories)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:455)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:422)
at org.hibernate.mapping.Property.isValid(Property.java:226)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:597)
at org.hibernate.mapping.RootClass.validate(RootClass.java:265)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:451)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889)
... 46 more

I checked annotation package references, and all ID column names, but all of them are correct. point : I also use the HSQLDB database whose storage location it is a file.

2

2 Answers

2
votes

I found my problem. mapping class in persistence.xml was incorrect.

1
votes

You annotations infer property access type mapping as @Id is placed on getter

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "FOOD_ID", nullable = false)
@Override
public Long getId() {
    return id;
}

But collection annotaions are placed on field level

@ManyToMany
@JoinTable(
    name = "T_FOOD_FOODCATEGORY",
    joinColumns =       { @JoinColumn(name = "FOOD_ID") }, 
    inverseJoinColumns =    { @JoinColumn(name = "FOOD_CATEGORY_ID") })
private Set<FoodCategoryEntity> categories;

While effective configuration is (not) specified on getter

public Set<FoodCategoryEntity> getCategories() {
    return categories;
}

You should not mix two types of configurations.