2011-12-28 101 views
0

我有一點問題。hql,休眠 - 插入關聯表

我有兩個表池和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,但有場(poolid)這與池表相關聯。

在POJO字段(poolid)被映射爲「池」。

我如何可以插入值到PoolQuestion,becouse這樣的代碼:

PoolQuestion poolQuestion = new PoolQuestion(); 

    Transaction transaction = session.beginTransaction(); 

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

    session.save(poolQuestion); 

    transaction.commit(); 

不起作用。

請幫忙!

+1

什麼是PoolQuery?爲什麼當它需要一個池的實例時將一個int傳遞給setPool? – 2011-12-28 23:37:48

+0

您已將PoolQuestion映射爲關係的所有者,因此要保存此PoolQuestion,只需在其上設置Pool並保存即可。 – 2011-12-28 23:39:05

+0

好吧,但如果我嘗試'poolQ​​uestion.setPool(新池());'我得到錯誤:'org.hibernate.PropertyValueException:非空屬性引用空值或瞬態值:com.pool.app.domain。 PoolQuestion.pool' – Ilkar 2011-12-28 23:42:01

回答

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 

如果池必須創建以及問題,那麼你必須實例化一個新的池,保存它,並做與上面相同的事情。

+0

謝謝你的幫助 - 那是關鍵 – Ilkar 2011-12-29 10:04:43

0

首先,不通過int,但是Pool實例到PoolQuestion.setPool()

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

希望幫助:你保存PoolQuestion或使用級聯保存之前那麼無論先救Pool實例。