0 Replies Latest reply on Jun 15, 2007 12:39 PM by nigelwhite

    @SecondaryTable. How to use it?

    nigelwhite

      How can I use @SecondaryTable?

      I have a Contact entity which defines a person's email, phone etc. There may be several of these pointing to a single Person entity.

      Because a person may belong to several organizations, and have several contactable personas.

      So. I want to create a ContactPerson entity from two tables.

      It seems that I have to have the primary table as the Person table because there are many Contacts, and they have a foreign key which points to the Person's primary key, and it only works this way round, the seconary pointing to the primary. You can't have a FK on the primary pointing to the secondary!!!

      But obviously, the @Id field cannot be the id field of that primary table, the Person table. Because it will be repeated several times, once for each Contact that's linked to it.

      so I have

      @javax.persistence.Table(name="Person")
      @SecondaryTable(name="ContactDetails",
       pkJoinColumns=@PrimaryKeyJoinColumn(name="person_id"),
       uniqueConstraints = @UniqueConstraint(columnNames={"person_id", "player_id"})
      )
      public class ContactPerson extends CompositeEntity implements Serializable
      {
       private Long id;
       @Column(table="ContactDetails", name="id")
       @Id
       public Long getId() {
       return id;
       }
       public void setId(Long id) {
       this.id = id;
       }
       ...
      


      And I think it's that ID that's causing the problem.

      With this, I get

      org.hibernate.MappingException: Unable to find logical column name from physical name id in table person
       org.hibernate.cfg.Mappings.getLogicalColumnName(Mappings.java:514)
       org.hibernate.cfg.Ejb3JoinColumn.linkValueUsingDefaultColumnNaming(Ejb3JoinColumn.java:270)
       org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:214)
       org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1253)
       org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1101)
       org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:567)
       org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:508)
       org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
       org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
       org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
       org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
       org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1233)