2

I have 2 simple tables in database. They look like:

T1                                      T2
ID    NUMBER     Primary key            ID    NUMBER    Primary key & Foreign key from T1
Value VARCHAR                           Value VARCHAR

How does hibernate entity for T2 look like? I tried to do it with @Embeddable class containing T1 mapped-class object but this doesn't work. Thanks.

UPD: the full use case when I need such a structure is below: I have business entities tables, containing some data for specific business users, and Company table with Id and value fields too, and I want to create CompanyToBEntity table, containing data about what company can access what object.(objects are any row of bus.entities).

So I think this structure fits this case.

pic describing it better:

enter image description here

Anton Dozortsev
  • 4,782
  • 5
  • 34
  • 69
AndreyKle
  • 53
  • 1
  • 6
  • if ID in T2 is primary and foreign key; then why do you require T2 at all? – Balwinder Singh Oct 15 '15 at 05:43
  • added my use case, will try to add link to picture, to describe the case better – AndreyKle Oct 15 '15 at 05:54
  • Refer http://stackoverflow.com/questions/6833370/jpa-onetoone-with-shared-id-can-i-do-this-better you have to use OneToOne & MapsId – Dev Blanked Oct 15 '15 at 06:13
  • Another option is to Map one Entity to the two tables using the @SecondaryTable annotation. https://en.wikibooks.org/wiki/Java_Persistence/Tables#Multiple_tables – Alan Hay Oct 15 '15 at 10:43

1 Answers1

1

You can try Unidirectional one-to-one association vi primary key association something like -

T1 Mapping

@Id  
@Column(name="ID")  
private Integer ID; 

T2 Mapping

@OneToOne(cascade=CascadeType.ALL)  
@PrimaryKeyJoinColumn  
private T1 t1;

For more reference you visit here, the example is based on hbm.xml

subodh
  • 6,136
  • 12
  • 51
  • 73