JPA @ Many-To-Many具有多余的列和@OrderColumn删除不会更新@OrderColumn值

时间:2018-11-28 16:43:07

标签: hibernate jpa

我有实体

属性

@Entity
@Table(name = "Attributes")
public class Attribute implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Version
    private Long version = 0L;

    private String name = "";

    @Getter @Setter
    @OneToMany(mappedBy = "attribute", orphanRemoval = true)
    private Set<AttributeGroup> groups = new HashSet<>(0);

    // getters - setters
    // hashcode - equals, based on id
}

@Entity
@Table(name = "Groups")
public class Group implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Version
    private Long version = 0L;

    private String name = "";

    @OneToMany(mappedBy = "group", orphanRemoval = true, cascade = {PERSIST, MERGE})
    @OrderColumn(name = "ordinal", nullable = false, columnDefinition = "INT DEFAULT 0")
    private List<AttributeGroup> attributes = new ArrayList<>(0);

    // getters - setters
    // hashcode - equals, based on id
}

AttributeGroup / AttributeGroupId

@Entity
@Table(name = "AttributesGroups")
public class AttributeGroup implements Serializable {

    @EmbeddedId
    private AttributeGroupId id;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("groupId")
    @JoinColumn(name = "groupId", foreignKey = @ForeignKey(name = "FK_ATTRIBUTES_ATTRIBUTES_GROUPS"))
    private Group group;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("attributeId")
    @JoinColumn(name = "attributeId", foreignKey = @ForeignKey(name = "FK_ATTRIBUTES_GROUPS_ATTRIBUTES"))
    private Attribute attribute;

    public AttributeGroup(final Group group, final Attribute attribute) {
        this.id = new AttributeGroupId(group.getId(), attribute.getId());
        this.group = group;
        this.attribute = attribute;
    }

    // getters - setters
    // hashcode - equals, based on id
}

@Embeddable
class AttributeGroupId implements Serializable {

    private Long groupId;

    private Long attributeId;

    public AttributeGroupId() { }

    private AttributeGroupId(final Long groupId, final Long attributeId) {
        this.groupId = groupId;
        this.attributeId = attributeId;
    }

    // getters - setters
    // hashcode - equals, based on groupId and attributeId
}

向我拥有的组中添加一些属性

+---------------------------------+
| groupId | attributeId | ordinal |
+---------------------------------+
|    4    |      1      |    0    |
|---------------------------------|
|    4    |      2      |    1    |
|---------------------------------|
|    4    |      3      |    2    |
+---------------------------------+

当我从Group(4)的attributes列表中删除Attribute(2)并保存该组时,一切正常并且我拥有

+---------------------------------+
| groupId | attributeId | ordinal |
+---------------------------------+
|    4    |      1      |    0    |
|---------------------------------|
|    4    |      3      |    1    |
+---------------------------------+

但是当我按原样删除/删除Attribute(2)时,它将其从关联的Group(4)中删除,但是列序号没有得到更新,所以我有了

+---------------------------------+
| groupId | attributeId | ordinal |
+---------------------------------+
|    4    |      1      |    0    |
|---------------------------------|
|    4    |      3      |    2    |
+---------------------------------+

我的地图出问题了吗?

是否可以通过使用EntityListeners来解决此问题?

我正在使用Hibernate 5.3.1.Final作为JPA Provider

0 个答案:

没有答案