JavaPersistence - relationship maping
DataBase - Postgres 8.2
AppServ - Sun Application Server
I use Hibernate.
Below is structure of my Postgres db :
CREATE SEQUENCE x_id_seq;
CREATE TABLE x (
id integer NOT NULL DEFAULT nextval('x_id_seq') PRIMARY KEY,
ad_typesmallint
);
CREATE TABLE x2 (
fk_x_idinteger NOT NULL REFERENCES x(id) PRIMARY KEY,
contactvarchar(100)
);
In my application is standard Unidirectional OneToOne relation.
To write something in table x and x2 I must first call method which call native query Select nextval('x_id_seq'), save response, build new x and x2 object and after this set manually x.id and x2.fk_x_id x.setX2( x) and em.persist(x)
My question is: Am I have to do this in way like this? Maybe exsists other simple solution? I would like do this like below:
X x = new X();
X2 x2 = new X2();
x2.setContact("sdfs")
x.setAdType(0);
x.setX2(x2);
transaction.begin()
em.persist(x);
transaction.commit()
Is this possible ? When I tried do something like this I get error: There is no Id or somethin
[1168 byte] By [
marcinta] at [2007-11-27 10:59:34]

# 1
Hi Marc,
for the sequence you can use:
@javax.persistence.Entity
@javax.persistence.SequenceGenerator(name="xSeqGen", sequenceName="x_id_seq")
class x
{
// attribute id
@javax.persistence.Id // => NOT NULL PRIMARY KEY
@javax.persistence.GeneratedValue(strategy=SEQUENCE, generator="HivesSeqGen") // => DEFAULT nextval('x_id_seq')
private int id;
public int getId() { return id; }
public void setId( int id ) { this.id = id; }
For using x as the pk to x2 you can try using @javax.persistence.IdClass at x itself and put an @OneToOne on the reference to field x within x2, also mark it as @Id.
If that does not wirk, create a new class which only has an x member. Apply the @OneToOne to the x field within this new class. In that case this new class has to get the @IdClass annotation instead of x directly.
Keep in mind: you might wanna use @Column(name="ad_type") to map adType/getAdType() to ad_type.
I hope you can figure it out with these hints. Please tell me if it helped.
... MIchael
# 2
Thx for answer.
So in fact the solution with SequenceGenerator / GeneratedValue is ok and works fine.
But I can't understand how can I simply insert the same value of sequence generator to entity x and x2.
I have now something like this:
@Entity
@SequenceGenerator(name="xSeqGen", sequenceName="x_id_seq")
@Table(name = "x")
@NamedQuery..........
public class X implements Serializable {
@Id
@GeneratedValue(strategy=SEQUENCE, generator="xSeqGen" )
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "ad_type")
private Short adType;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "x")
private X2 x2;
.....
And X2
@Entity
@Table(name = "x2")
@NamedQueries( {...... })
public class X2 implements Serializable {
@Id
@Column(name = "fk_x_id", nullable = false)
private Integer fkXId;
.....
@JoinColumn(name = "fk_x_id", referencedColumnName = "id", insertable = false, updatable = false)
@OneToOne
private X x;