我有一个美国实体,其id是主键,userId和ServiceId这些最后两个一起也是唯一的。现在我有一个userId和一个serviceId,我想更新一个美国实体,我不知道使用hibernate做正确的语法。请有人帮助我吗... 我已经使用注释进行实体映射:
@Id
@Column(name = "USERSERVICE_ID")
@GeneratedValue
private Integer id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "SERVICE_ID")
private Service service;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "SERVICE_SEND_TYPE_ID")
private ServiceSendType serviceSendType;
@Column(name = "USERSERVICE_LASTSENT")
private String lastSentValue = "";
@Column(name = "USERSERVICE_UNSUBCTIMESTAMP")
private Date unsubscribeDate;
@Column(name = "USERSERVICE_SUBCTIMESTAMP")
private Date subscribeDate;
@Column(name = "USERSERVICE_ENABLED")
private Boolean enabled = false;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "SERVICE_USER_ID")
private User user;
答案 0 :(得分:1)
试试这个:
US entity = (US) session.createQuery("select s from US u where u.service.id = :serviceId and u.user.id = :userId")
.setParameter("serviceId", serviceId)
.setParameter("userId", userId)
.uniqueResult();
//update
entity.setEnabled(true);
就是这样。从DB加载实体后,它会自动附加到当前会话,因此automatic dirty checking mechanism将检测到任何更改。