添加子关系(@ManyToMany集合)时如​​何使Hibernate不增加父实体的“版本”列

时间:2018-07-06 15:12:41

标签: java hibernate version optimistic-locking

表格:

create table myuser.A (
  id number primary key,
  notes varchar2(50),
  version number);

create table myuser.B (
  id number primary key,
  username varchar2(50),
  version number);

create table myuser.A_B (
  a_id number,
  b_id number,
  constraint a_fk foreign key (a_id) references A(id),
  constraint b_fk foreign key (b_id) references B(id)
);

insert into myuser.A values ('1', 'notes', 1);
insert into myuser.B values ('44', 'username', 1);
insert into myuser.A_B values ('1', '44');

注释休眠映射:

@Entity
class A {

  @Id
  @Column
  public String id;

  @Column
  public String notes;

  @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinTable(name = "A_B",
      joinColumns = { @JoinColumn(name = "A_ID") },
      inverseJoinColumns = { @JoinColumn(name = "B_ID") })
  public Set<B> bDomains;

  @Version
  public int version;
}

@Entity
class B {

  @Id
  @Column
  public String id;

  @Column
  public String username;

  @ManyToMany(fetch = FetchType.EAGER, mappedBy = "bDomains")
  public Set<A> aDomains;

  @Version
  public int version;
}

获取现有的“ A”对象,并将新的子对象添加到ManyToMany集合中。因此,“ A”对象不会改变,但是Hibernate会为其生成一个新的版本号并更新“ A”表:

A a = session.get(A.class, "1");
int versionBefore = a.version;

B b = new B();
b.id = "48";
b.username = "asd";

a.bDomains.add(b);

session.save(a);
session.flush();

int versionAfter = a.version;
System.out.println("version before: " + versionBefore);
System.out.println("version after: " + versionAfter);

输出:

Hibernate: select a0_.id as id1_0_0_, a0_.notes as notes2_0_0_, a0_.version as version3_0_0_, bdomains1_.A_ID as A_ID1_1_1_, b2_.id as B_ID2_1_1_, b2_.id as id1_2_2_, b2_.username as username2_2_2_, b2_.version as version3_2_2_ from A a0_, A_B bdomains1_, B b2_ where a0_.id=bdomains1_.A_ID(+) and bdomains1_.B_ID=b2_.id(+) and a0_.id=?
Hibernate: select adomains0_.B_ID as B_ID2_1_0_, adomains0_.A_ID as A_ID1_1_0_, a1_.id as id1_0_1_, a1_.notes as notes2_0_1_, a1_.version as version3_0_1_ from A_B adomains0_, A a1_ where adomains0_.A_ID=a1_.id and adomains0_.B_ID=?
Hibernate: select b_.id, b_.username as username2_2_, b_.version as version3_2_ from B b_ where b_.id=?
Hibernate: insert into B (username, version, id) values (?, ?, ?)
Hibernate: update A set notes=?, version=? where id=? and version=?
Hibernate: insert into A_B (A_ID, B_ID) values (?, ?)
version before: 1
version after: 2

我不需要对由休眠生成的表进行UPDATE查询,因为在此逻辑中表未更改。 据我所知,Hibernate在更改实体的任何字段时都会递增版本列,即使该字段只是对另一个表的引用。如果仅更改实体的关系,如何配置Hibernate以防止其版本增加?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。 需要在@org.hibernate.annotations.OptimisticLock(excluded = true)属性上添加@ManyToMany|@OneToMany注释。

因此,我示例中的A实体现在看起来像这样:

@Entity
class A {

  @Id
  @Column
  public String id;

  @Column
  public String notes;

  @org.hibernate.annotations.OptimisticLock(excluded = true)
  @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinTable(name = "A_B",
      joinColumns = { @JoinColumn(name = "A_ID") },
      inverseJoinColumns = { @JoinColumn(name = "B_ID") })
  public Set<B> bDomains;

  @Version
  public int version;
}

生成的SQL:

Hibernate: select a0_.id as id1_0_0_, a0_.notes as notes2_0_0_, a0_.version as version3_0_0_, bdomains1_.A_ID as A_ID1_1_1_, b2_.id as B_ID2_1_1_, b2_.id as id1_2_2_, b2_.username as username2_2_2_, b2_.version as version3_2_2_ from A a0_, A_B bdomains1_, B b2_ where a0_.id=bdomains1_.A_ID(+) and bdomains1_.B_ID=b2_.id(+) and a0_.id=?
Hibernate: select adomains0_.B_ID as B_ID2_1_0_, adomains0_.A_ID as A_ID1_1_0_, a1_.id as id1_0_1_, a1_.notes as notes2_0_1_, a1_.version as version3_0_1_ from A_B adomains0_, A a1_ where adomains0_.A_ID=a1_.id and adomains0_.B_ID=?
Hibernate: select b_.id, b_.username as username2_2_, b_.version as version3_2_ from B b_ where b_.id=?
Hibernate: insert into B (username, version, id) values (?, ?, ?)
Hibernate: insert into A_B (A_ID, B_ID) values (?, ?)
version before: 1
version after: 1

唯一的问题是OptimisticLock批注来自Hibernate,而不是JPA。