0

我在使用 struts2 的 Hibernate 中遇到 OneToMany 关系的问题

我有 2 个模型对象 UserDetails 和 Vehicle,我在这 2 个对象之间有一个双向的 OneToMany 关系。问题是我不能持久化 Vehicle Object 而 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>

任何想法我的错误在哪里?提前致谢。

4

1 回答 1

0

谢谢大家。现在我发现了我的错误。我正在实现 ModelDriven 类。所以我有重复的车辆值。一个来自动作类,第二个来自模型本身。并且 struts 无法确定将输入值附加到何处... :)

于 2012-05-11T12:00:47.520 回答