两个带有Struts 2.0操作的数据库表

时间:2014-02-21 06:46:00

标签: java hibernate jsp struts2

我试图用一个Struts 2.0 Action类将值保存到两个数据库表中。

我已经尝试过实施ModelDriven但它并没有抓住这两个VO。请给我一个解决方案,以便在同一个struts 2.0动作类中捕获两个VO。

UserVO.java的代码片段

package com.sajeewi.struts.vo;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "user")
public class UserVO {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "idUser", unique = true, nullable = false)
    private int idUser;

    @Column(name = "username", unique = true, nullable = false, length = 20)
    private String username;

    @Column(name = "password", unique = true, nullable = false, length = 20)
    private String password;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "userVO", cascade = CascadeType.ALL)
    private User_DetailsVO user_DetailsVO;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "userVO")
    private Set<AccountVO> accountVOs;

    public UserVO() {
        // TODO Auto-generated constructor stub
    }   

    public UserVO(int idUser,String username,String password){
        this.idUser = idUser;
        this.username = username;
        this.password = password;
    }   

    public int getIdUser() {
        return idUser;
    }
    public void setIdUser(int idUser) {
        this.idUser = idUser;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public User_DetailsVO getUser_DetailsVO() {
        return user_DetailsVO;
    }
    public void setUser_DetailsVO(User_DetailsVO user_DetailsVO) {
        this.user_DetailsVO = user_DetailsVO;
    }

    public Set<AccountVO> getAccountVOs() {
        return accountVOs;
    }

    public void setAccountVOs(Set<AccountVO> accountVOs) {
        this.accountVOs = accountVOs;
    }
}

这适用于User_DetailsVO.java

package com.sajeewi.struts.vo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;


@Entity
@Table(name="user_details")
public class User_DetailsVO {

    @Id
    @Column(name = "User_idUser", unique = true, nullable = false)
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "userVO"))   
    private int User_idUser;

    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    private UserVO userVO;

    @Column(name = "contact1", nullable = false, length = 15)
    private String contact1;

    @Column(name = "contact2", nullable = false, length = 15)
    private String contact2;

    @Column(name = "email", nullable = false, length = 50)
    private String email;

    @Column(name = "add1", nullable = false, length = 100)
    private String add1;

    @Column(name = "add2", nullable = false, length = 100)
    private String add2;

    @Column(name = "city", nullable = false, length = 50)
    private String city;

    public User_DetailsVO() {
        // TODO Auto-generated constructor stub
    }

    public User_DetailsVO(UserVO userVO,String contact1,String contact2,String email,String add1,String add2,String city){
        this.userVO = userVO;
        this.contact1 = contact1;
        this.contact2 = contact2;
        this.email = email;
        this.add1= add1;
        this.add2 = add2;
        this.city = city;
    }

    public int getUser_idUser() {
        return User_idUser;
    }

    public void setUser_idUser(int user_idUser) {
        User_idUser = user_idUser;
    }

    public UserVO getUserVO() {
        return userVO;
    }

    public void setUserVO(UserVO userVO) {
        this.userVO = userVO;
    }

    public String getContact1() {
        return contact1;
    }

    public void setContact1(String contact1) {
        this.contact1 = contact1;
    }

    public String getContact2() {
        return contact2;
    }

    public void setContact2(String contact2) {
        this.contact2 = contact2;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAdd1() {
        return add1;
    }

    public void setAdd1(String add1) {
        this.add1 = add1;
    }

    public String getAdd2() {
        return add2;
    }

    public void setAdd2(String add2) {
        this.add2 = add2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }



}

这是用户行动类

package com.sajeewi.struts.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sajeewi.struts.dao.UserDAO;
import com.sajeewi.struts.dao.impl.UserDAOImpl;
import com.sajeewi.struts.vo.UserVO;
import com.sajeewi.struts.vo.User_DetailsVO;

@SuppressWarnings({ "serial" })
public class UserAction extends ActionSupport implements ModelDriven<Object>{

    UserVO userVO = new UserVO();
    User_DetailsVO detailsVO = new User_DetailsVO();
    List<UserVO> userList = new ArrayList<UserVO>();
    UserDAO userDAO = new UserDAOImpl();


    public List<UserVO> getUserList() {
        return userList;
    }

    public void setUserList(List<UserVO> userList) {
        this.userList = userList;
    }

    public UserDAO getUserDAO() {
        return userDAO;
    }

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }   

    public UserVO getUserVO() {
        return userVO;
    }

    public void setUserVO(UserVO userVO) {
        this.userVO = userVO;
    }

    public User_DetailsVO getDetailsVO() {
        return detailsVO;
    }

    public void setDetailsVO(User_DetailsVO detailsVO) {
        this.detailsVO = detailsVO;
    }

    public String addUser(){
//      userDAO.saveUser(this.userVO);
//      userList = null;
//      try {
//          userList = userDAO.searchAllUsers();
//      } catch (Exception e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }

        System.out.println(this.userVO);
        System.out.println(this.detailsVO);

        return SUCCESS;
    }

    public Object getModel() {
        return userVO;

    }




}

Jsp View

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User</title>
</head>
<body>
    <s:div>
        <h2>Registration With Struts 2.0</h2>
        <s:form action="addUser" method="POST">
            <s:div>
                <h4>Enter User Credentials</h4>
                <s:textfield name="username" label="Username"></s:textfield>
                <s:password name="password" label="Password"></s:password>
            </s:div>
            <br>
            <s:textfield name="contact1" label="Contact1 "></s:textfield>
            <s:textfield name="contact2" label="Contact2 "></s:textfield>
            <s:textfield name="email" label="E-Mail "></s:textfield>
            <s:textfield name="add1" label="Address Line 1 "></s:textfield>
            <s:textfield name="add2" label="Address Line 2 "></s:textfield>
            <s:textfield name="city" label="City "></s:textfield>
            <br>
            <s:submit name="Save" value="Save"></s:submit>
        </s:form>
    </s:div>
</body>
</html>

我想保存到两个表格相同的表格

由于

2 个答案:

答案 0 :(得分:1)

好的,所以你有一个单独的表来存储用户信息,但所有信息都是从单个表单中读取的。在那种情况下,它很简单。

不要去模型驱动,因为所有表单字段都不存在于单个类中,否则你可以拥有相同的bean类(我不推荐它).Populate两个类对象中的相应值{{ 1}}和UserVO在动作类中使用getter n setter。

现在你要做的就是:

User_DetailsVO

我无法在你的表中猜出哪一个是主键,哪个是外键。因此保存主要对象,然后保存具有FK的其他对象。

答案 1 :(得分:0)

有一个解决方案。我已经改变了我的JSP

之前

<s:textfield name="username" label="Username"></s:textfield>

现在

<s:textfield name="userVO.username" label="Username"></s:textfield>

我所做的只是将VO名称添加到文本域名称,然后它会自动捕获UserVO和User_DetailsVO的动作类中的所有数据

谢谢