gpt4 book ai didi

java - 仅在保存实体时制作 transient 列

转载 作者:行者123 更新时间:2023-11-29 14:19:33 25 4
gpt4 key购买 nike

我有下一个情况。我有实体对象 User:

package models;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;

import org.hibernate.annotations.Proxy;


@Entity
@Table(name="users")
@Proxy(lazy=true)
public class User {

private int id;
private String login;
private String password;
private String name;
private String email;
private Integer age;
private String country;
private Set<UserRole> roles = new HashSet<UserRole>();
private UserStatus status;
private Date created;
private Date updated;

public User() {
status=UserStatus.A;
}

public User(String user_login, String user_password, String user_name, String user_email) {
this.login = user_login;
this.password = user_password;
this.name = user_name;
this.email = user_email;
status=UserStatus.A;
}

public User(String user_login, String user_password, String user_name, String user_email, int age) {
this(user_login, user_password, user_name, user_email);
this.age = age;
}

public User(String user_login, String user_password, String user_name, String user_email, int age, String country) {
this(user_login, user_password, user_name, user_email, age);
this.country = country;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="user_id", unique = true)
public int getId() {
return id;
}

public void setId(int user_id) {
this.id = user_id;
}

@Column(name="user_login")
public String getLogin() {
return login;
}

public void setLogin(String user_login) {
this.login = user_login;
}

@Column(name="user_password")
public String getPassword() {
return password;
}

public void setPassword(String user_password) {
this.password = user_password;
}

@Column(name="user_name")
public String getName() {
return name;
}

public void setName(String user_name) {
this.name = user_name;
}

@Column(name="user_email")
public String getEmail() {
return email;
}

public void setEmail(String user_email) {
this.email = user_email;
}

@Column(name="user_age")
public Integer getAge() {
return age;
}

public void setAge(Integer user_age) {
this.age = user_age;
}

@Column(name="user_country")
public String getCountry() {
return country;
}

public void setCountry(String user_country) {
this.country = user_country;
}

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "users_to_userroles", joinColumns = { @JoinColumn(name = "user_id") },
inverseJoinColumns = { @JoinColumn(name = "user_role_id ") })
public Set<UserRole> getRoles() {
return roles;
}

public void setRoles(Set<UserRole> user_roles) {
this.roles = user_roles;
}

@Column(name="user_status")
@Enumerated(EnumType.STRING)
public UserStatus getStatus() {
return status;
}

public void setStatus(UserStatus status) {
this.status = status;
}

@Transient
@Column(name="user_created")
public Date getCreated() {
return created;
}

public void setCreated(Date user_created) {
this.created = user_created;
}

@Transient
@Column(name="user_updated")
public Date getUpdated() {
return updated;
}

public void setUpdated(Date user_updated) {
this.updated = user_updated;
}

}

JSP 页面(简单的形式,与问题无关)使用创建新用户的表单和显示所有现有用户的表。我在表单和实体对象 User 之间使用了绑定(bind)(它在 Controller 内部):

User user = new User();
List<User> users = userService.getAllUsers();//to fill table with users
List<UserRole> userRoles = userRolesService.getAllRoles();//to fill tables with users

model.addAttribute("rolesList", userRoles);
model.addAttribute("users", users);
model.put("adminForm", user);//Here adminForm is the name of form in JSP page

现在问题是什么:如您所见,User 有两个字段 user_createduser_updated(它们由 Postgres 服务器自动创建)。它们与所有其他字段一起转发到 JSP 页面中的表。但是我在 JSP 中的表单不提供这些字段(不需要 - 对)))),因此当从表单传输到 Controller 时它们为空。现在 Hibernate 无法在 Postgres 服务器上添加行,因为两个字段为空(((所以我的问题是:我能以某种方式将这些列标记为 @Transient 但只有当我保存实体而不是从数据库中读取它时。我知道我仍然可以绑定(bind)单独的字段而不是整个对象。但是仍然可以按照我的要求做吗?使用现有配置,新的User 已保存,但未读取这两个字段且 JSP 表列为空(((

最佳答案

您需要将列映射的insertableupdatable 属性设置为false。这将使该字段对于 Hibernate 为只读。

@Column(name="user_created", insertable=false, updatable=false)

关于java - 仅在保存实体时制作 transient 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34069215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com