Hibernate在多个表中插入记录

时间:2014-10-29 06:05:35

标签: java hibernate struts2

我的情景是我有三个表格模板,问题和选择。

这里模板将有很多问题,问题将在选择表中有很多选择和其他字段。

我在Struts2中使用单一表单来插入记录。我的问题是如何在hibernate中配置???

谢谢!!!

@Entity
@Table(name="template")
public class Template {

@javax.persistence.Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="t_id")
private Integer Id;

private String temp;

@OneToMany(targetEntity=Question.class,  cascade=CascadeType.ALL)
@JoinColumn(name="tempId",  referencedColumnName="t_id")
private Set<Question> question;

//getter setter


 }

@Entity
@Table(name="question")
public class Question {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="Qid")
private Integer Qid;

private String question;

@ManyToOne
@JoinColumn(name="tempid")
private Template template;

@OneToMany(targetEntity=Choice.class, cascade=CascadeType.ALL)
@JoinColumn(name="qid", referencedColumnName="Qid")
private Set<Choice> choice;

//getter setter

}

@Entity
@Table(name="choice")
public class Choice {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer Id;

private String choice;

private String status;

private String description;

@ManyToOne
@JoinColumn(name="qid")
private Question question;

//getter setter

}

************控制器*****************

public class UserController extends ActionSupport implements ServletRequestAware{


HttpServletRequest request;

@Override
public void setServletRequest(HttpServletRequest request) {
    this.request =request;
}

Dao dao = new Dao();


Template template = new Template();

Question question = new Question();

Choice choice = new Choice();


public Choice getChoice() {
    return choice;
}


public void setChoice(Choice choice) {
    this.choice = choice;
}


public Question getQuestion() {
    return question;
}


public void setQuestion(Question question) {
    this.question = question;
}

public Template getTemplate() {
    return template;
}


public void setTemplate(Template template) {
    this.template = template;
}

public String insert(){
    System.out.println(choice);
    SessionFactory sf = Dbutil.getConnection();
    Session session = sf.openSession();
    Transaction tx =  session.beginTransaction();

    Choice ch1 = new Choice();
    ch1.setChoice(choice.getChoice());
    ch1.setStatus(choice.getStatus());
    ch1.setDescription(choice.getDescription());

    Set<Choice> chset =  new HashSet<Choice>();
    chset.add(choice);

    Set<Question> qns  = new HashSet<Question>();
    qns.add(question);

    Question q = new Question();
    q.setQuestion(question.getQuestion());
    q.setChoice(chset);

    Template t = new  Template();
    t.setTemplate(template.getTemplate());
    t.setQuestion(qns);

    dao.insertemplate(t,q, ch1);
    //session.save(t);
    //session.save(q);
    //session.save(ch1);

    tx.commit();
    session.flush();

    return "success";
}

}

******************* DAO ******************

    public class Dao {


    public void insertemplateT(Template template, Question qn1, Choice ch1 ){

    SessionFactory sf = Dbutil.getConnection();
    Session session = sf.openSession();
    Transaction tx =  session.beginTransaction();

    if(ch1!=null){
        insertChoice(ch1);
        if(qn1!=null){

            insertQuestion(qn1 );

            if(template !=null){
                session.save(template);
                tx.commit();
            }
        }
    }   

    }

    public void insertQuestion(Question qn1 ) {
    SessionFactory sf = Dbutil.getConnection();
    Session session = sf.openSession();
    Transaction tx =  session.beginTransaction();

    session.save(qn1);
    tx.commit();
    }



    public void insertChoice(Choice ch1) {
    SessionFactory sf = Dbutil.getConnection();
    Session session = sf.openSession();
    Transaction tx =  session.beginTransaction();
    session.save(ch1);
    tx.commit();
    System.out.println("committed");

    }



    }

*********** sql show ************

  

Hibernate:插入选择(选择,描述,qid,状态)值(?,?,?,?)   提交   Hibernate:插入问题(question,tempid)值(?,?)   Hibernate:插入选择(选择,描述,qid,状态)值(?,?,?,?)   Hibernate:更新选择集qid =? Id =?   Hibernate:插入模板(temp)值(?)   Hibernate:插入问题(question,tempid)值(?,?)   Hibernate:更新问题集tempId =? Qid =?

******问题表图片****** enter image description here

1 个答案:

答案 0 :(得分:0)

@Entity
@Table(name="template")
public class Template {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="t_id")
private Integer Id;

private String temp;

@OneToMany(mappedBy="template",  cascade=CascadeType.ALL)
private Set<Question> questions;

}




@Entity
@Table(name="question")
public class Question {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="Qid")
private Integer Qid;

private String question;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="tempid")
private Template template;

@OneToMany(mappedBy="question", cascade=CascadeType.ALL)
private Set<Choice> choices;

public void addChoice(Choice choice){
    choices.add(choice);
    choice.setQuestion(this);
}

public void setTemplate(Template template){
    this.template = template;
    template.getQuestions.add(this);
}


@Entity
@Table(name="choice")
public class Choice {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer Id;

private String choice;

private String status;

private String description;

@ManyToOne
@JoinColumn(name="qid")
private Question question;

}


Template template = //;
Choice choice = //;

Question question = new Question();
question.addChoice(choise);
question.setTemplate(template);

    public void insertQuestion(Question qn1) {
    SessionFactory sf = Dbutil.getConnection();
    Session session = sf.openSession();
    Transaction tx =  session.beginTransaction();

    session.save(qn1);
    tx.commit();
}