hql,hibernate - 插入关联表

时间:2011-12-28 22:51:02

标签: hibernate hql pojo

我有一点问题。

我有两个表Pool和PoolQuestion:

游泳池:

    package com.pool.app.domain;

// Generated 2011-12-28 23:05:46 by Hibernate Tools 3.4.0.CR1

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Pool generated by hbm2java
 */
@Entity
@Table(name = "pool", catalog = "pool")
public class Pool implements java.io.Serializable {

    private Integer id;
    private String name;
    private String slug;
    private Date dateCreate;
    private Date deactivationDate;
    private int creatorId;
    private int active;
    private Set<PoolQuestion> poolQuestions = new HashSet<PoolQuestion>(0);

    public Pool() {
    }

    public Pool(String name, String slug, int creatorId, int active) {
        this.name = name;
        this.slug = slug;
        this.creatorId = creatorId;
        this.active = active;
    }

    public Pool(String name, String slug, Date dateCreate,
            Date deactivationDate, int creatorId, int active,
            Set<PoolQuestion> poolQuestions) {
        this.name = name;
        this.slug = slug;
        this.dateCreate = dateCreate;
        this.deactivationDate = deactivationDate;
        this.creatorId = creatorId;
        this.active = active;
        this.poolQuestions = poolQuestions;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "name", nullable = false, length = 200)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "slug", nullable = false, length = 200)
    public String getSlug() {
        return this.slug;
    }

    public void setSlug(String slug) {
        this.slug = slug;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "date_create", length = 19)
    public Date getDateCreate() {
        return this.dateCreate;
    }

    public void setDateCreate(Date dateCreate) {
        this.dateCreate = dateCreate;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "deactivation_date", length = 19)
    public Date getDeactivationDate() {
        return this.deactivationDate;
    }

    public void setDeactivationDate(Date deactivationDate) {
        this.deactivationDate = deactivationDate;
    }

    @Column(name = "creator_id", nullable = false)
    public int getCreatorId() {
        return this.creatorId;
    }

    public void setCreatorId(int creatorId) {
        this.creatorId = creatorId;
    }

    @Column(name = "active", nullable = false)
    public int getActive() {
        return this.active;
    }

    public void setActive(int active) {
        this.active = active;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pool")
    public Set<PoolQuestion> getPoolQuestions() {
        return this.poolQuestions;
    }

    public void setPoolQuestions(Set<PoolQuestion> poolQuestions) {
        this.poolQuestions = poolQuestions;
    }

}

和PoolQuestion:

 package com.pool.app.domain;

// Generated 2011-12-28 23:05:46 by Hibernate Tools 3.4.0.CR1

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * PoolQuestion generated by hbm2java
 */
@Entity
@Table(name = "pool_question", catalog = "pool")
public class PoolQuestion implements java.io.Serializable {

    private Integer id;
    private Pool pool;
    private String answer;
    private int order;
    private String question;
    private int value;

    public PoolQuestion() {
    }

    public PoolQuestion(Pool pool, String answer, int order, int value) {
        this.pool = pool;
        this.answer = answer;
        this.order = order;
        this.value = value;
    }

    public PoolQuestion(Pool pool, String answer, int order, String question,
            int value) {
        this.pool = pool;
        this.answer = answer;
        this.order = order;
        this.question = question;
        this.value = value;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "poolid", nullable = false)
    public Pool getPool() {
        return this.pool;
    }

    public void setPool(Pool pool) {
        this.pool = pool;
    }

    @Column(name = "answer", nullable = false, length = 500)
    public String getAnswer() {
        return this.answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    @Column(name = "order", nullable = false)
    public int getOrder() {
        return this.order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    @Column(name = "question", length = 500)
    public String getQuestion() {
        return this.question;
    }

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

    @Column(name = "value", nullable = false)
    public int getValue() {
        return this.value;
    }

    public void setValue(int value) {
        this.value = value;
    }

}

我想在PoolQuery中插入值,但是有一个与Pool表相关联的字段(poolid)。

在POJO中,字段(poolid)被映射为“池”。

如何在PoolQuestion中插入值,因为这样的代码:

PoolQuestion poolQuestion = new PoolQuestion();

    Transaction transaction = session.beginTransaction();

    poolQuestion.setAnswer(answer);
    poolQuestion.setPool(1);
    poolQuestion.setValue(0);

    session.save(poolQuestion);

    transaction.commit();

不起作用。

请帮忙!

2 个答案:

答案 0 :(得分:2)

您想要将问题与现有池相关联。此池具有ID 1,并且位于数据库中。所以你不想创建一个新池。 new Pool()因此不是你想要的。该池位于数据库中。要从数据库中获取实体,必须使用Hibernate会话。因此,您要求Hibernate会话从数据库中获取ID为1的池:

Pool pool = session.get(Pool.class, 1);

然后,您必须将池与问题相关联。由于关联是双向的,因此您还希望池包含这个新问题:

question.setPool(pool);
pool.getQuestions.add(question);

最后,您希望将问题保留在数据库中:

session.persist(question); // or save

如果必须与问题一起创建池,那么您必须实例化一个新池,保存它,并执行与上面相同的操作。

答案 1 :(得分:0)

首先,不要将int而是Pool个实例传递给PoolQuestion.setPool()。然后在保存Pool之前先保存PoolQuestion实例,或使用级联保存

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.SAVE_UPDATE)
@JoinColumn(name = "poolid", nullable = false)
public Pool getPool() {
  return this.pool;
}

希望有所帮助。