entityManager.merge不更新数据

时间:2017-05-10 12:56:36

标签: java merge jpa-2.0

我正在尝试使用entitymanager.merge对数据库中的现有实体进行简单更新,但它不会对数据库进行更改。

这是我的代码:

实体:

@Entity
@Table(name = "User")
@SequenceGenerator(name = "tirGen", initialValue = 1, allocationSize = 100)
public class User implements Serializable {
@Id
@SequenceGenerator(name = "USER_SEQ", sequenceName = "USER_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_SEQ")
@Column(name = "ID")
private long              id;

@NotNull
@Column(name = "VALEUR_STATUT", nullable = false, insertable = true, updatable = true)
private String            status;
// GETTERS and Setters
}

我正在调用merge(如果我合并一个没有Id的新实体,它与Id一起使用它不会进行更改)

// Entity manager is already injected
final EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
  if (entityManager == null) {
    throw new RollbackException("Unable to obtain a transactional EntityManager");
  }

  final EntityTransaction entityTransaction = entityManager.getTransaction();

  if (entityTransaction == null) {
    throw new DataAccessResourceFailureException("Unable to obtain a EntityTransaction");
  }

  // Ouverture de la transaction
  entityTransaction.begin();
  entityManager.flush();
  entityManager.clear();

  if (entityTransaction.isActive()) {               
    User user2850 = entityManager.find(User.class, 2850L);
    user2850.setStatus("NewValue");
    User mergedUser = entityManager.merge(user2850);

    // when I debug on mergedUser I get status = newValue
    entityManager.flush();
    // Commit de la transaction
    entityTransaction.commit();
    // No modification in the database the user with Id 2850 have the old status

正如我所提到的,它在合并一个新实体时起作用(我试图更新另一个属性但它仍然不起作用)

0 个答案:

没有答案