struts2 を使用した Hibernate での OneToMany 関係に問題があります。
UserDetails と Vehicle の 2 つのモデル オブジェクトがあり、この 2 つのオブジェクト間に双方向の OneToMany 関係があります。そして問題は、 UserDetails が永続化されているのに Vehicle Object を永続化できないことです。コンパイル エラーや実行時エラーはありません... vehicle テーブルが空です
ここに私のクラスがあります:
UserDetails.java
package org.project.model;
/*imports... */
@Entity
@Table(name="user_details")
public class UserDetails {
private int userId;
private String userName;
private Set<Vehicle> vehicle;
@Id
@GeneratedValue()
@Column(name="id")
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@Column(name="name")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@OneToMany(mappedBy="user")
public Set<Vehicle> getVehicle() {
return vehicle;
}
public void setVehicle(Set<Vehicle> vehicle) {
this.vehicle = vehicle;
}
}
車両.java
package org.project.model;
/*imports... */
@Entity
@Table(name="vehicle")
public class Vehicle {
private int vehicleId;
private String vehicleName;
private UserDetails user;
@Id
@GeneratedValue()
@Column(name="vehicle_id")
public int getVehicleId() {
return vehicleId;
}
public void setVehicleId(int vehicleId) {
this.vehicleId = vehicleId;
}
@Column(name="vehicle_name")
public String getVehicleName() {
return vehicleName;
}
public void setVehicleName(String vehicleName) {
this.vehicleName = vehicleName;
}
@ManyToOne
@JoinColumn(name="user_details")
public UserDetails getUser() {
return user;
}
public void setUser(UserDetails user) {
this.user = user;
}
}
アクションクラス
package org.project.actions;
/*imports...*/
public class UserAction extends ActionSupport implements ModelDriven<UserDetails>{
private static final long serialVersionUID = -2167265047934268752L;
private UserDetails user = new UserDetails();
private UserDetailsDao userDao = new UserDetailsDaoImpl( );
private List<UserDetails> userList = new ArrayList<UserDetails>();
/** PREVIOUS MISTAKE **/
private Vehicle vehicle = new Vehicle();
@Override
public UserDetails getModel(){
return user;
}
public String add() throws Exception {
/** PREVIOUS MISTAKE **/
vehicle.setUser(user);
user.getVehicle().add(vehicle);
userDao.saveUser( user );
return SUCCESS;
}
public String list() {
userList = userDao.listUser();
return SUCCESS;
}
public String details() throws Exception {
user = userDao.getUser();
return SUCCESS;
}
public String populate() throws InterruptedException{
//...some population code...
return "populate";
}
public String execute(){
return SUCCESS;
}
public UserDetails getUser() {
return user;
}
public void setUser(UserDetails user) {
this.user = user;
}
public List<UserDetails> getUserList() {
return userList;
}
public void setUserList(List<UserDetails> userList) {
this.userList = userList;
}
}
およびjspページ
<!-- Head Section -->
<body>
<s:form action="saveUser">
<s:textfield name="userName" label="User Name"/>
<s:textfield name="vehicle.vehicleName" label="Vehicle Name" />
<s:submit value="Save"/>
</s:form>
</body>
</html>
私の間違いはどこですか?前もって感謝します。