-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Setting foreign key values without loading the class
PostPosted: Wed Sep 29, 2010 4:45 am 
Newbie

Joined: Mon Sep 27, 2010 12:32 am
Posts: 5
When you have an annotated class and you build a record (of that class) for insert and it has several foreign keys the records of which already exist ... how do you insert the proper values into the annotated class?

Example:

Table A
pk_a
fld1_a
fld2_a

Table B
pk_b
fld1_b

Table C
pk_c
fk_a
fk_b

My java class for Table C looks like this:

public class C {
private int cId;
private A a;
private B b;
private String Field1;

@Id
@Column(name = "C_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.Auto)
public int getCId() {
return cId;
}
public void setCId( int cId){
this.cID = cId;
}

@ManyToOne(fetch = FetchType.LAZY, targetEntity = database_tables.A.class)
@JoinColumn(name = "A_ID", nullable = false)
public A getA() {
return this.a;
}
public void setA(A a) {
this.a = a;
}

@ManyToOne(fetch = FetchType.LAZY, targetEntity = database_tables.B.class)
@JoinColumn(name = "B_ID", nullable = false)
public B getB() {
return this.b;
}
public void setB(B b) {
this.b = b;
}

// Field getters and setters (blah blah)
}

OK, suppose I have a fully filled out table A and table B. Now I want to insert a record into table C. What is the build code for the table C record? I have something like this:

Session session = null;
Transaction transaction = null;
try{
System.out.println("Here 1");
// This step will read hibernate.cfg.xml
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
// Create new instance of Database a record map and set
// values in it by reading them from the form object.

C c = new C();

c.Field1 = whatever;

// What do I do to reference the existing records in the A and B tables?
// I have form data that can properly indicate the foreign key values but
// my annotated C class has only A and B class setters.

session.save(c);
transaction.commit();

System.out.println("Done");

}catch(Throwable ex){
transaction.rollback();
System.out.println(ex.getMessage());
}finally{
// Actual insertion/cleanup will happen now.
System.out.println("finally ...");
if ( session != null ){
session.flush();
session.close();
} // if
} // finally
} // try

Thanks for any help. I am not sure here what I need help on, SQL, or Hibernate annotations, or JPA.


Top
 Profile  
 
 Post subject: Re: Setting foreign key values without loading the class
PostPosted: Wed Sep 29, 2010 5:54 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
// What do I do to reference the existing records in the A and B tables?


You load the existing A and B using session.get() or session.load() and then call C.setA() and C.setB(). Something like this:

Code:
A a = (A)session.load(A.class, aId);
c.setA(a);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.