简体   繁体   English

更新jpa中实体的所有字段

[英]update all fields of an entity in jpa

I have tow entity User and Project that they have "one to many" relationship and I want to find the User then find the specific Project that belong to User and then update it, but I can't. 我必须拖曳实体User和Project,它们具有“一对多”关系,我想找到User,然后找到属于User的特定Project,然后更新它,但是我不能。 framework Struts2 + Hibernate . 框架Struts2 + Hibernate。

@Entity (name = "User")
@Table (name = "users")
public class User implements Serializable{
@Id
@Column (name = "user_id", columnDefinition = "number")
@SequenceGenerator(name = "seq", sequenceName = "gen")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq")
private int id;

@Basic
@Column(name = "user_name", columnDefinition = "nvarchar2(20)")
private String userName;

@Basic
@Column(name = "password", columnDefinition ="nvarchar2(20)")
private String password;

@Basic
@Column(name = "create_date",columnDefinition = "date")
private Date creation_date;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private List<Project> projectses;

public List<Project> getProjectses() {
    return projectses;
}

public void setProjectses(List<Project> projectses) {
    this.projectses = projectses;
}

and Project entity 和项目实体

@Entity(name = "Project")
@Table(name = "project")
public class Project implements Serializable {
@Id
@Column(name = "project_id" , columnDefinition = "number")
@SequenceGenerator(name = "projectSeq", sequenceName = "projectGen")
@GeneratedValue(strategy = GenerationType.AUTO,generator = "projectSeq")
private int projectId;

@Basic
@Column(name = "project_name" , columnDefinition = "nvarchar2(20)")
private String projectName;

@Basic
@Column(name = "project_description" , columnDefinition = nvarchar2(20)")
private String projectDescription;

@Basic
@Column(name = "start_date",columnDefinition = "date")
private Date startDate;

@Basic
@Column(name = "due_date",columnDefinition = "date")
private Date dueDate;

@Basic
@Column(name = "project_status",columnDefinition = "nvarchar2(20)")
private String projectStatus;

@Basic
@Column(name = "project_amount",columnDefinition = "number(8)")
private int projectAmount;
User u = (User)entityManager.createQuery(SELECT u FROM User u JOIN FETCH u.Project where u.id = :id).setParameter("id",your_userId).uniqueResult();

Get The user object , you will get it with the set of projects associated with that user 获取用户对象,您将通过与该用户关联的项目集来获取它

update the data you want : - 更新您想要的数据:-

List<Project> userProjects = u.getProjectses();   
for(int i = 0 ; i < userProjects.size() ; i++){
   Project p = userProjects.get(i);
   entityManager.getTransaction().begin();
   p.setProjectName("test"); 
   entityManager.merge(p); 
   entityManager.getTransaction().commit(); 
}

Three corrections come in mind, that may make your code behave well: 请记住三个更正,它们可能会使您的代码表现良好:

  • Use EntityManager#find() instead of HQL to lookup entity by id; 使用EntityManager#find()代替HQL通过id查找实体;

  • Use only one of @Basic or @Column (I'd prefer @Column ), there is no need to use both for a single element; 仅使用@Basic@Column (我希望使用@Column ),一个元素无需同时使用两者;

  • If error is " value is too long for column ", maybe it is time to check if some of Project elements are longer than 20 chars defined in columnDefinition ? 如果错误是“ 列的值太长 ”,也许是时候检查一些Project元素是否长于columnDefinition定义的20个字符了?

Check length of string elements in Project entities, and if there are some longer than 20 characters, say, 500, modify columnDefinition for those elements, eg: 检查Project实体中字符串元素的长度,如果长度超过20个字符(例如500个),请为这些元素修改columnDefinition ,例如:

@Column(name = "project_description", columnDefinition = "nvarchar2(500)")
private String projectDescription;

I also suggest to drop tables after such modifications (to allow JPA create new according to new definitions) or manually modify their definitions in DB. 我还建议修改后删除表(以允许JPA根据新定义创建新表)或在DB中手动修改其定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM