You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate多对多映射报错org.hibernate.MappingException:无法确定java.util.Set类型问题求助

Fixing Hibernate Many-to-Many Mapping Exception

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:

  1. Missing @ManyToMany annotation on the owning side
    In your ManagerOva class, you've added the @JoinTable annotation to the availSenss field, but you forgot to mark it as a @ManyToMany association. 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).

  2. Mismatched join table name
    You mentioned you manually created the join table as vm_ova_sensors, but your @JoinTable annotation uses name = "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_sensors has the exact column names ova_id (FK to manager_ova.id) and sensor_id (FK to sens.id), matching the @JoinColumn annotations.
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 17:47:49