Primary key composed of two foreign keys

Asked

Viewed 217 times

-1

Good afternoon to you all! I have a doubt in the structure of an Entity, I am using Hibernate+JPA. The class is a user rating, where one primary key will be the user (coming from the User class), and the other is the season (coming from the season class). Below is the class structure:

@Entity(name = "CLASSIFICACAO")
@Data
public class Classificacao implements Serializable
{
  @Id
  @GeneratedValue(generator="SharedPrimaryKeyGenerator")
  @GenericGenerator(name="SharedPrimaryKeyGenerator",strategy="foreign",parameters =  @Parameter(name="property", value="usuario"))
  @Column(unique = true, nullable = false)
  private String classificacao;
  @Id
  @GeneratedValue(generator="SharedPrimaryKeyGenerator")
  @GenericGenerator(name="SharedPrimaryKeyGenerator",strategy="foreign",parameters =  @Parameter(name="property", value="temporada"))
  @Column(unique = true, nullable = false)
  private Long codTemporada;
  //another fields  
  @OneToOne
  @PrimaryKeyJoinColumn
  private Usuario usuario;
  @ManyToOne
  @PrimaryKeyJoinColumn
  private Temporada temporada;
}

The field classification receives user name from foreign key User; Before I didn’t have the field codTemporated as a primary, and it worked beautifully. Rating received user and was primary, and the season only foreign. But now I need the field season is also primary making a class of primary keys composed. But only pops the error Broken column Mapping for: usuario.id of: br.com.xxxxx.model..

Some light of what I can do ?

1 answer

0

I found the solution to my problem! Follow below for those who need:

Classificacao.

@Entity
public class Classificacao implements Serializable {

    @Id
    ClassificacaoId id;

    String name;
    // getter and setter

}

Classificacaoid.java

@Embeddable
public class ClassificacaoId implements Serializable {

    @OneToOne
    @JoinColumn(name = "userId", referencedColumnName = "userId")
    private Usuario usuarioIo;

    @ManyToOne
    @JoinColumn(name = "tempId", referencedColumnName = "id")
    private Temporada temporada;

    // getter and setter

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.