我有下一个情况。我是实体对象用户:
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 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_created 和 user_updated (它们由Postgres服务器自动创建)。它们将所有其他字段转发到JSP页面中的表。但我在JSP中的表单不提供这些字段(不需要 - 右)))),因此当从表单转换为控制器时它们为空。现在Hibernate无法在Postgres服务器上添加行,因为两个字段都是空的((所以我的问题是: 我可以以某种方式将这些列标记为 @Transient ,但仅当我保存实体不从数据库中读取它时。 我知道我仍然可以在形式上绑定单独的字段而不是整个对象。但仍然有可能做我的要求吗?使用现有配置,将保存新的用户,但不会读取这两个字段,并且JSP表列为空((
答案 0 :(得分:2)
您需要将列映射的可插入和可更新属性设置为false。这将使该字段对Hibernate只读。
@Column(name="user_created", insertable=false, updatable=false)