четверг, 3 июля 2014 г.

Hibernate : mapping entities without foreign key association

Got a challenge today.
Let's say we have some tables with classificator data, and we need to store translations of the classificators.
One solution is to get a translation table with fields:

- ID (generated Primary Key)
- TABLE_NAME (for which table the translation is created)
- TABLE_ID (PrimaryKey value for particular classificator recordof )
- LANG
- TRANSLATION_VALUE (some text in particular language)

The problem is that there is no any foreign key between Classificator table and Translation table.
I thried to solve the problem using @Loader annotation, but it seems that this functionality is buggy.

The final solution, which seems to work, is using @JoinColumnsOrFormulas annotation:

 

@Entity @Table(name = "i18n_translation")
public class I18nTranslation extends AbstractManagedEntity {

  @Id  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  @Column(name = "table_name") private String tableName;
  @Column(name = "table_id") private String tableId;
  @Column(name = "lang_code", nullable = false) private String lang;

  @Basic @Column private String translation;

 ///Boilerplate ///
}

@Entity @Table( name="producer_type")
public class ProducerType extends AbstractDeletableEntity{

  @Id private Long id;
  @Basic private String descr;

  @OneToMany(fetch = FetchType.EAGER)
  @Where(clause = "table_name='producer_type'")
  @JoinColumnsOrFormulas( {@JoinColumnOrFormula(column = @JoinColumn(name = "table_id")) } )
  private Set translations;
 ///Boilerplate ///
}


Комментариев нет:

Отправить комментарий