Hibernate多对多映射报错org.hibernate.MappingException:无法确定java.util.Set类型问题求助
Hey there, let's work through this issue together—those Hibernate mapping errors can be tricky, but we'll get it sorted!
The Root Causes
Looking at your code and the exception message, there are two key issues causing the Could not determine type for: java.util.Set error:
Missing
@ManyToManyannotation on the owning side
In yourManagerOvaclass, you've added the@JoinTableannotation to theavailSenssfield, but you forgot to mark it as a@ManyToManyassociation. Hibernate can't recognize this as a relational mapping without that annotation, so it treats it like a regular field (which it can't map to a database column).Mismatched join table name
You mentioned you manually created the join table asvm_ova_sensors, but your@JoinTableannotation usesname = "manager_ova_sens". These need to match exactly for Hibernate to find the correct table.
Bonus: Stable Equals/HashCode Implementation
Right now, your equals() and hashCode() methods include the associated collections (ovas and availSenss). This can cause issues with lazy loading (Hibernate might fetch collections unnecessarily) and unstable hash codes if the collection changes. It's better to base these methods solely on your entity's primary key (id), which is immutable once set.
Modified Code Examples
Updated ManagerOva Class
@Entity @Table(name = "manager_ova") @Getter @Setter @Recoverable(backupOption = BackupOption.BASIC, fileDependecies = {}) public class ManagerOva implements Serializable { private static final long serialVersionUID = 2994716515279921578L; @Id @Column(unique = true, nullable = false) private Long id; @Column(unique = false, nullable = false) private String name; // Added @ManyToMany annotation, fixed join table name @ManyToMany @JoinTable( name = "vm_ova_sensors", // Matches your manually created table joinColumns = @JoinColumn(name = "ova_id"), inverseJoinColumns = @JoinColumn(name = "sensor_id") // Matches your FK column name ) private Set<Sens> availSenss; public ManagerOva() {} // Simplified equals/hashCode using only the primary key @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ManagerOva other = (ManagerOva) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } }
Updated Sens Class (Equals/HashCode Fix)
@Entity @Table(name = "sens") @Getter @Setter @Recoverable(backupOption = BackupOption.BASIC, fileDependecies = {}) public class Sens implements Serializable { private static final long serialVersionUID = 5622870770478762574L; @Id @Column(unique = true, nullable = false) private Long id; @Column(unique = false, nullable = false) private String name; @ManyToMany(mappedBy = "availSenss") private Set<ManagerOva> ovas; public Sens() {} // Simplified equals/hashCode using only the primary key @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Sens other = (Sens) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } }
Final Checks
- Double-check that your join table
vm_ova_sensorshas the exact column namesova_id(FK tomanager_ova.id) andsensor_id(FK tosens.id), matching the@JoinColumnannotations. - Ensure that both entities are properly scanned by Hibernate (they should be in a package included in your persistence configuration).
That should resolve the mapping exception and make your many-to-many association work correctly!
内容的提问来源于stack exchange,提问作者Gal Sosin

