JonH
JonH

Reputation: 501

hibernate annotations and joins

I am trying to use hibernate to retrieve data from my database...I have a query that joins two tables (all fields being one to one) using a left join. I need to know how to handle this with hibernate and annotations. I have created two classes that map to tables and I am trying to figure out how to use the joins...Hibernate docs are confusing...

//one class

@Entity
@Table(name = "Class1")
public class Class1{

 @Id
 @Column(name = "INITIAL")
 private String initial;

  @Column(name = "NUMBER")
  private Integer number;

...

//twoclass

@Entity
@Table(name = "Class2")
public class Class2{

 @Column(name = "STATE")
 private String state;

  @Id   
  @Column(name = "NUMBER")
  private Integer number

...

Upvotes: 0

Views: 1440

Answers (1)

Ilya
Ilya

Reputation: 29703

@Entity
@Table(name = "Class1")
public class Class1 {
   @Id
   @Column(name = "INITIAL")
   private String initial;

   @Column(name = "NUMBER")
   private Integer number;  
}


@Entity
@Table(name = "Class2")
public class Class2 {
   @Id
   @Column(name = "STATE")
   private String state;

   @Column(name = "NUMBER")
   private Integer number

   @OneToOne
   @JoinColumn(name = "columnWithClass1id")
   private Class1 class1;
}

Upvotes: 2

Related Questions