简体   繁体   中英

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. framework 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;

  • Use only one of @Basic or @Column (I'd prefer @Column ), there is no need to use both for a single element;

  • 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 ?

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:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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