我在使用struts2
的Hibernate中遇到OneToMany关系问题我有2个模型对象UserDetails和Vehicle,我在这个2对象之间有一个双向的OneToMany关系。问题是我不能持久存在Vehicle Object而UserDetails是持久存在的。没有编译或运行时错误......只是车辆表是空的
以下是我的课程:
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;
}
}
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>
任何想法都是我的错误?提前谢谢。
答案 0 :(得分:0)
谢谢大家。现在我发现了我的错误。我正在实现ModelDriven类。所以我有重复的车辆价值。一个来自Action类,第二个来自模型本身。并且struts无法确定将输入值附加到何处......:)