使用Hibernate更新连接表中的值

时间:2014-10-03 16:57:12

标签: java mysql sql hibernate

鉴于此实体类

@Entity
public class Profile extends Model {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;
  ...
  @OneToMany(fetch=FetchType.EAGER)
  @Cascade(value={CascadeType.ALL})
  @Fetch(FetchMode.SELECT)
  private Set<Expectation> expectations;
  ...
}

对Profile对象进行的更新操作不会影响期望集(映射到连接表)。

我使用以下代码:

Session dbSession = DBHandler.getDbSession(); // custom static method

Profile profile = (Profile) dbSession.get(Profile.class, id);

Set<Expectation> expectations = new HashSet<Expectation>();
for (Long id : expectationsIds)
  expectations.add((Expectation) dbSession.get(Expectation.class, id));
profile.setExpectations(expectations);

dbSession.beginTransaction();
dbSession.saveOrUpdate(profile);
dbSession.getTransaction().commit();

dbSession.close();

0 个答案:

没有答案