hql,hibernate-插入到关联表中

hql,hibernate-插入到关联表中,hibernate,hql,pojo,Hibernate,Hql,Pojo,我有个小问题 我有两张桌球和台球问题: 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.Generated

我有个小问题

我有两张桌球和台球问题:

 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;
    }

}
游泳池:

    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;
    }

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

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

我如何在PoolQuestion中插入值,因为代码如下:

PoolQuestion poolQuestion = new PoolQuestion();

    Transaction transaction = session.beginTransaction();

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

    session.save(poolQuestion);

    transaction.commit();
不起作用


请帮忙

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


希望这会有所帮助。

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

Pool pool = session.get(Pool.class, 1);
session.persist(question); // or save
然后,您必须将池与问题相关联。由于关联是双向的,因此您还希望池中包含以下新问题:

question.setPool(pool);
pool.getQuestions.add(question);
最后,您希望将问题持久化到数据库中:

Pool pool = session.get(Pool.class, 1);
session.persist(question); // or save

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

什么是池查询?当setPool获取一个Pool实例时,为什么要向它传递一个int?您已经将PoolQuestion映射为关系的所有者,所以要保存这个PoolQuestion,您只需要在其上设置池,然后保存它。好的,但是如果我尝试使用
PoolQuestion.setPool(new Pool())我得到错误:
org.hibernate.PropertyValueException:notnull属性引用空值或暂时值:com.pool.app.domain.PoolQuestion.pool
感谢您的帮助-这是关键