Search code examples
hibernatehibernate-mapping

hibernate mapping confusion


Could someone explain me why this doesn't work? I want to have a table that contains informations about two users who played with each other. This doesn't work.

Public class History implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ManyToOne
    @Column(nullable = false)
    private UserDetails player1;

    @ManyToOne
    @Column(nullable = false)
    private UserDetails player2;

    @ManyToOne
    @Column(nullable = false)
    private UserDetails winner;

    @Column(nullable = false)
    private Date time;

}

What am I doing wrong and how to fix it?


Solution

  • @Column is used to specify the mapping of a basic type. To specify the mapping of an association to another entity, you use a @JoinColumn (or a @JoinTable, depending on what you want):

    @ManyToOne
    @JoinColumn(nullable = false)
    private UserDetails player1;