-->
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: Question about mapping what seems like a simple OneToOne
PostPosted: Tue Dec 30, 2008 1:17 pm 
Newbie

Joined: Fri Oct 31, 2008 5:40 pm
Posts: 7
As an example, I have something like the following structure:

create table foo (
foo_id int,
data text
primary key (foo_id)
);

create table bar (
foo_id int,
data text,
constraint bar_foo_fk foreign key (foo_id) references foo on delete no action
primary key (foo_id)
);

I can't get this to work. One doesn't inherit from the other, it's just related data

public class Foo {
private Integer id;
private String data;

@Id
public getId() {return id;}

public getData() {return data;}
}

public class Bar {
private Foo foo;
private String data;

public getFoo() {return foo;}

public getData() {return data;}
}

of course it complains about a missing @Id. But if I put an Id on getFoo() it tells me it can't determine type of Foo. What is the right way to do this?


Versions:
Hibernate Annotations 3.3.0.GA
Hibernate 3.2.6
MySQL 5.0.x
Java 6 on MS Windows
IDE is IntelliJ 8.0.1


Top
 Profile  
 
 Post subject: Question about mapping what seems like a simple OneToOne JPA
PostPosted: Tue Dec 30, 2008 3:26 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
Hello plexq, it looks like you have missing mappings.

    -> You need to declare that both classes are entities with the @Entity annotation.

    -> You need to use the @Table annotation to specify the name of the table of the database.

    -> The @OneToOne annotation on either side of the mapping or both if you want it to have a bidirectional mapping.

    -> By default the JPA annotations are looked on the property fields not on the mutator methods.

    -> You may need an id generator strategy for id generation.


With all of the above you will have something like this

Code:
@Entity
@Table(name="foo")
public class Foo {

   @Id
   private Integer id;

   private String data;

   // setters/getters 

}

@Entity
@Table(name="bar")
public class Bar {

    @OneToOne
    private Foo foo;   

    private String data;

    // setters /getters

}



Regards,


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.