i have a little problem.
I have two tables Pool and PoolQuestion:
Pool :
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;
}
}
and 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;
}
}
I want to insert values into PoolQuery, but there is field (poolid) which is associated with Pool table.
In POJO the field (poolid) was mapped as "Pool".
How can i insert value into PoolQuestion, becouse code like this:
PoolQuestion poolQuestion = new PoolQuestion();
Transaction transaction = session.beginTransaction();
poolQuestion.setAnswer(answer);
poolQuestion.setPool(1);
poolQuestion.setValue(0);
session.save(poolQuestion);
transaction.commit();
doesn't work.
Please help!
You want to associate a question with an existing pool. This pool has ID 1, and is in the database. So you don't want to create a new pool. new Pool()
is thus not what you want. The pool is in the database. To get entities from the database, the Hibernate session is what must be used. So you ask the Hibernate session to get the pool with ID 1 from the database:
Pool pool = session.get(Pool.class, 1);
Then you must associate the pool to the question. Since the association is bidirectional, you also want the pool to contain this new question:
question.setPool(pool);
pool.getQuestions.add(question);
And finally, you want the question to be persisted in database:
session.persist(question); // or save
If the pool must be created along with the question, then you must instantiate a new pool, save it, and do the same thing as above.