PDA

View Full Version : Hibernate one to one



gerch123
Jun 8th, 2009, 07:48 PM
Hi guys

another question. I can see that roo supports one to many relationships in hibernate.

how about one to one?


also is there a way to create the sql scripts from the entity objects?

Thank you

Stefan Schmidt
Jun 9th, 2009, 09:21 PM
Hi,

To create a relationship you could use the 'add field reference jpa -type com.foo.Pet -fieldName myPet -class com.foo.Owner'. This will generate a field like this in Owner.java:



@ManyToOne
@JoinColumn
private Pet myPet;


We are reviewing if it makes more sense to use JPA's @OneToOne relationship instead.



also is there a way to create the sql scripts from the entity objects?


I am not clear what exactly you are trying to do. But if you want to create your own query you could just use the JPA entityManager which is already existent in your JPA @Entity:



public List<Owner> test(){
return entityManager.createQuery("select p from Owner p").getResultList();
}


Is this what you are referring to?

-Stefan

gerch123
Jun 10th, 2009, 09:04 AM
Hey Stefan

thanks for the answer.

The second part was more about if i can create "table statements" like create table Owner (id auto_increment int primary key, name varchar(50))

from roo and its managed entities...

Thanks

btw where are you guys located? you have a very german name :)

Stefan Schmidt
Jun 10th, 2009, 07:24 PM
Hi,

For the feature of executing SQL scripts upon application deployment you can take a look at the petclinic samples in Spring framework. They create tables and pre-populate the tables upon application start if that is what you are referring to. In the upcoming Spring 3 release we will also include a new feature which let's you configure that very conveniently for embedded databases via the application context.

Ben and I are based in Sydney, Australia, but indeed I am German :).


-Stefan

gerch123
Jun 11th, 2009, 08:39 AM
Nice thank you... i will take a look at those samples...