我正在寻找如何使用jpacontainer在我的实体项目上进行更新。该项目是唯一的,我想通过此项目更新。
@Entity
@Table(name="curriculum")
public class Curriculum implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long idCurriculum;
@Size(min=5, max=50)
@NotNull
@NotEmpty
private String nome;
@Email
@NotEmpty
@NotNull
@Size(max=250)
@Column(unique=true)
private String email;
@NotEmpty
@NotNull
@Size(min=8, max=8)
private String senha;
}
在这种情况下,我想使用唯一密钥的电子邮件更新senha(密码)
有什么想法吗?
/ **已编辑* /
我设法使用Criteria。我不知道这是否正确,但现在它是有效的。
这里,我是怎么做的。
public void changePassword(String email, String password){
try{
EntityManager em = datasource.getEntityProvider().getEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Curriculum> c = cb.createQuery(Curriculum.class);
Root<Curriculum> curriculum = c.from(Curriculum.class);
c.where(
cb.equal(curriculum.get("email"), email)
);
TypedQuery q = em.createQuery(c);
Curriculum ce = (Curriculum)q.getSingleResult(); //ce = curriculum entity
datasource.getItem(ce.getIdCurriculum()).getItemProperty("password").setValue(password);
}catch(Exception e){
Notification.show("Curriculum not found \n",
e.getLocalizedMessage(),
Type.ERROR_MESSAGE);
}
}
还有另一个解决方案,只使用JPAContainer更简单吗?
答案 0 :(得分:0)
由于电子邮件不是Id字段,我首先使用Query(键入,命名或本机)来获取要更新的Curriculum对象,然后我将通过EntityManager.merge更新它:
EntityManager em = ...
TypedQuery<Curriculum> query = em.createQuery(
"SELECT c FROM Curriculum c " +
"WHERE c.email = :p1",
Curriculum.class);
query.setParameter("p1", "myemail@myhost.com");
Curriculum curriculum = (Curriculum) query.getSingleResult();
curriculum.setSenha("mypwd");
em.merge(curriculum);