在我不确定要更改哪个字段的情况下,如何使用休眠更新?
How do I use update using hibernate where i am not sure on which field is to be altered?
public User updateUser(User user) {
try {
User result = session.get(User.class, user.getId());
if (result == null) {
throw new FilamentNoSuchRecordException(new CoreError(304, "User does not exist"));
}
session.clear();
session.update(user);
return user;
} catch (HibernateException e) {
e.printStackTrace();
}
throw new FilamentDataConnectivityException(new CoreError(305,"Connectivity issue. Please see System Administrator"));
}
客户型号如下
@Entity
@Table(name = "customers")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@DynamicUpdate(value=true)
@SelectBeforeUpdate(value=true)
@SQLDelete(sql="Update customers SET deleted = true where customer_id=?")
@Where(clause="deleted != true")
@ApiModel(description="Create or update Customers")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Customer {
@Id
@Column(name="customer_id")
@NotNull
@GeneratedValue(strategy=GenerationType.AUTO)
private int id = 0;
@Column(name="name")
@ApiModelProperty(value="The name of the customer", example="Mr J. Bond")
@NotNull
private String name;
@Column(name="description")
@ApiModelProperty(value="Desciption of the customer")
@NotNull
private String description;
@Column(name="logo_url")
@ApiModelProperty(value="Logo of user")
@NotNull
private String logo;
@Column(name="created_at")
@ApiModelProperty(value="The date the item was created", example="")
@NotNull
private Date createdAt;
@Column(name="updated_at")
@ApiModelProperty(value="The date the item was updated", example="")
@NotNull
private Date updatedAt;
@ApiModelProperty(hidden=true)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private Set<Application> applications = new HashSet<Application>();
@ManyToMany(mappedBy = "customers")
private Set<Service> services = new HashSet<Service>();
@ApiModelProperty(hidden=true)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private Set<User> users = new HashSet<User>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "customer_subscription",
joinColumns = @JoinColumn(name = "customer_id"),
inverseJoinColumns = @JoinColumn(name = "subscription_id")
)
private Set<Subscription> subscriptions = new HashSet<Subscription>();
@ApiModelProperty(hidden=true)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private Set<Corpus> corpus = new HashSet<Corpus>();
@Column(name="deleted")
@NotNull
private boolean deleteFlag;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Application> getApplications() {
return applications;
}
public void setApplications(Set<Application> applications) {
this.applications = applications;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
public Set<Service> getServices() {
return services;
}
public void setServices(Set<Service> services) {
this.services = services;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Set<Corpus> getCorpus() {
return corpus;
}
public void setCorpus(Set<Corpus> corpus) {
this.corpus = corpus;
}
public Set<Subscription> getSubscriptions() {
return subscriptions;
}
public void setSubscriptions(Set<Subscription> subscriptions) {
this.subscriptions = subscriptions;
}
public boolean getDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(boolean deleteFlag) {
this.deleteFlag = deleteFlag;
}
}
我检查对象是否存在于数据库中,然后使用对象进行更新,例如,除了 ID 和需要更新的字段外,所有字段都可以为空。模型中的所有字段都设置为@NotNull,我正在使用@DynamicUpdate(value=true) 和@SelectBeforeUpdate(value=true) 注释,但这些似乎什么都不做。
只是失败说空字段不能为空。如何更新行?
正如我们在上面的评论中所讨论的那样,试试这个 -
public User updateUser(User user) {
try {
User result = session.get(User.class, user.getId());
if (result == null) {
throw new FilamentNoSuchRecordException(new CoreError(304, "User does not exist"));
}
result.setName(user.getName()); // update some properties
session.update(result); // you should update 'result', not 'user'
return result;
} catch (HibernateException e) {
e.printStackTrace();
throw new FilamentDataConnectivityException(new CoreError(305,"Connectivity issue. Please see System Administrator"));
}
}
通过使用我在另一个堆栈溢出中发现的这个方法post解决了这个问题。这将检查每个字段并使用 'not null' 值。然后我可以从一个只更改了 1 个字段的对象进行更新。
public static <T> T getNotNull(T a, T b) {
return b != null && a != null && !a.equals(b) ? a : b;
}
public User updateUser(User user) {
try {
User result = session.get(User.class, user.getId());
if (result == null) {
throw new FilamentNoSuchRecordException(new CoreError(304, "User does not exist"));
}
session.clear();
session.update(user);
return user;
} catch (HibernateException e) {
e.printStackTrace();
}
throw new FilamentDataConnectivityException(new CoreError(305,"Connectivity issue. Please see System Administrator"));
}
客户型号如下
@Entity
@Table(name = "customers")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@DynamicUpdate(value=true)
@SelectBeforeUpdate(value=true)
@SQLDelete(sql="Update customers SET deleted = true where customer_id=?")
@Where(clause="deleted != true")
@ApiModel(description="Create or update Customers")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Customer {
@Id
@Column(name="customer_id")
@NotNull
@GeneratedValue(strategy=GenerationType.AUTO)
private int id = 0;
@Column(name="name")
@ApiModelProperty(value="The name of the customer", example="Mr J. Bond")
@NotNull
private String name;
@Column(name="description")
@ApiModelProperty(value="Desciption of the customer")
@NotNull
private String description;
@Column(name="logo_url")
@ApiModelProperty(value="Logo of user")
@NotNull
private String logo;
@Column(name="created_at")
@ApiModelProperty(value="The date the item was created", example="")
@NotNull
private Date createdAt;
@Column(name="updated_at")
@ApiModelProperty(value="The date the item was updated", example="")
@NotNull
private Date updatedAt;
@ApiModelProperty(hidden=true)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private Set<Application> applications = new HashSet<Application>();
@ManyToMany(mappedBy = "customers")
private Set<Service> services = new HashSet<Service>();
@ApiModelProperty(hidden=true)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private Set<User> users = new HashSet<User>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "customer_subscription",
joinColumns = @JoinColumn(name = "customer_id"),
inverseJoinColumns = @JoinColumn(name = "subscription_id")
)
private Set<Subscription> subscriptions = new HashSet<Subscription>();
@ApiModelProperty(hidden=true)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private Set<Corpus> corpus = new HashSet<Corpus>();
@Column(name="deleted")
@NotNull
private boolean deleteFlag;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Application> getApplications() {
return applications;
}
public void setApplications(Set<Application> applications) {
this.applications = applications;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
public Set<Service> getServices() {
return services;
}
public void setServices(Set<Service> services) {
this.services = services;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Set<Corpus> getCorpus() {
return corpus;
}
public void setCorpus(Set<Corpus> corpus) {
this.corpus = corpus;
}
public Set<Subscription> getSubscriptions() {
return subscriptions;
}
public void setSubscriptions(Set<Subscription> subscriptions) {
this.subscriptions = subscriptions;
}
public boolean getDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(boolean deleteFlag) {
this.deleteFlag = deleteFlag;
}
}
我检查对象是否存在于数据库中,然后使用对象进行更新,例如,除了 ID 和需要更新的字段外,所有字段都可以为空。模型中的所有字段都设置为@NotNull,我正在使用@DynamicUpdate(value=true) 和@SelectBeforeUpdate(value=true) 注释,但这些似乎什么都不做。
只是失败说空字段不能为空。如何更新行?
正如我们在上面的评论中所讨论的那样,试试这个 -
public User updateUser(User user) {
try {
User result = session.get(User.class, user.getId());
if (result == null) {
throw new FilamentNoSuchRecordException(new CoreError(304, "User does not exist"));
}
result.setName(user.getName()); // update some properties
session.update(result); // you should update 'result', not 'user'
return result;
} catch (HibernateException e) {
e.printStackTrace();
throw new FilamentDataConnectivityException(new CoreError(305,"Connectivity issue. Please see System Administrator"));
}
}
通过使用我在另一个堆栈溢出中发现的这个方法post解决了这个问题。这将检查每个字段并使用 'not null' 值。然后我可以从一个只更改了 1 个字段的对象进行更新。
public static <T> T getNotNull(T a, T b) {
return b != null && a != null && !a.equals(b) ? a : b;
}