How to map an entity with compound key using JPA?

1
@Entity

@Table(name = "USUARIO")

public class Usuario implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @Column(name = "USER_NAME", nullable = false)
    private String userName;
}

@Entity

@Table(name = "CARGO")

public class Cargo implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @Column(name = "NAME", nullable = false)
    private String name;
}

@Entity

@Table(name = "USUARIO_CARGO")

public class Cargo implements Serializable {

    @JoinColumn(name = "USARIO_ID")
    private User user;

    @JoinColumn(name = "CARGO_ID")
    private Cargo cargo;
}
    
asked by anonymous 03.05.2017 / 16:49

1 answer

0

I do not know if this is what you want but it's there

@Entity
@Table(name = "USUARIO_CARGO")
public class usuario_Cargo implements Serializable {

    @Id
    @JoinColumn(name = "USARIO_ID", referencedColumnName= "id")
    private User user;

    @Id
    @JoinColumn(name = "CARGO_ID", referencedColumnName= "id")
    private Cargo cargo;
}
    
03.05.2017 / 21:45