无法确定类型:java.util.Set,在表中

Posted

技术标签:

【中文标题】无法确定类型:java.util.Set,在表中【英文标题】:Could not determine type for: java.util.Set, at table 【发布时间】:2017-11-11 09:47:32 【问题描述】:

我创建了两个具有多对多关系的类,如下所示:

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



和:

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



但是当我用 junit、spring 和 hibernate 测试这种关系时,我得到以下异常:

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

我检查了注解包引用和所有 ID 列名,但它们都是正确的。 要点:我也使用HSQLDB数据库,它的存储位置是一个文件。

【问题讨论】:

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]的可能重复 【参考方案1】:

我发现了我的问题。 persistence.xml 中的映射类不正确。

【讨论】:

【参考方案2】:

您的注释推断属性访问类型映射,因为@Id 放置在 getter 上

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

但是集合注释放在字段级别

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

虽然(未)在 getter 上指定有效配置

public Set<FoodCategoryEntity> getCategories() 
    return categories;

您不应混合使用两种类型的配置。

【讨论】:

当我将类别字段上的注释位置更改为方法时,我得到以下异常:原因:org.hibernate.AnnotationException:使用 OneToMany 或 ManyToMany 以未映射的类为目标:ir.msr.projects.crawler。 food.entity.FoodEntity.categories[ir.msr.projects.crawler.food.entity.FoodCategoryEntity] 第二个不扫描。测试配置问题?

以上是关于无法确定类型:java.util.Set,在表中的主要内容,如果未能解决你的问题,请参考以下文章

org.hibernate.MappingException:无法确定类型:java.util.Set [重复]

多对多关系:org.hibernate.MappingException:无法确定类型:java.util.Set

org.hibernate.MappingException:无法确定类型:java.util.Set,表:USERS,列:[org.hibernate.mapping.Column(invoices

org.hibernate.MappingException:无法确定类型:java.util.List,在表:用户,列:[org.hibernate.mapping.Column(事件)]

java.util.Set 的等效模式数据类型是啥?

检查字符串的第一部分是否在表中的值列表中