Hibernate Simple JoinTable without using entity

I am trying to start a simple JoinTable operation in Hibernate and it gives me the option. I have one table that represents an object called "Person". I have another table that represents social security numbers (for example). I want the social security number as a String (it is VARCHAR2) to be a PersonEntity property. PersonEntity works fine without code. Here is what I am trying to do.

@JoinTable(name = "PERSON_ID_X_SSN", 
               joinColumns = {@JoinColumn(name = "PERSON_ID")})
    @Column(name="SSN", nullable=false, updatable=false)
    private String social;

      

The PERSON_ID_X_SSN table is simple:

PERSON_ID, SSN

      

Everything works fine without this code. Once I add it to the whole entity, the value is null. I'd rather not create a separate entity for PERSON_ID_X_SSN ... please help! Thanks to

+3


source to share


1 answer


@JoinTable is for specifying a table that is used to maintain links between objects.

In this case, there is no relationship between the objects, but one entity that must be stored in two tables. @SecondaryTable is an annotation that targets servers for this purpose. The idea is to specify an additional table (or more than one via @SecondaryTables) for the object and explicitly specify the table name in the @Column annotation when a non-primary table should be used to store the attribute.

In your case, something like the following will work:



@Entity
@Table(name="CUSTOMER")
@SecondaryTable(
  name="PERSON_ID_X_SSN", 
  pkJoinColumns=@PrimaryKeyJoinColumn(name="PERSON_ID"))
public class Person {
  @Column(table="PERSON_ID_X_SSN", name="SSN")
  private String social;
  ...
}

      

}

+3


source







All Articles