Q:

Jpa strange value to long exception on simple join table

I have 2 entities project and user:
@Entity
public class Project implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Version
    @Column(name = "version")
    private int version;

    @Column(unique = true)
    private String name;

    @Column
    private String description;
}

and
@Entity
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Version
    @Column(name = "version")
    private int version;

    @Column(unique = true)
    private String name;

}

I want to create a join table entity (projects assigned to users) and want to use this in bookings entity. I used @IdClass approach (i got same result with embedable):
@Entity
@IdClass(UserProjectKey.class)
public class UserProjects implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @ManyToOne
    private User user;

    @Id
    @ManyToOne
    private Project project;

    public UserProjects() {

    }

    public UserProjects(final User user, final Project project){
        this.user = user;
        this.project = project;

    }
}

public class UserProjectKey implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long user;

    private Long project;

    public UserProjectKey() {

    }

    public UserProjectKey(final long userId, final long projectId){
        user = userId;
        project = projectId;
    }

}

And in booking this is used:
@Entity
public class Booking implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Version
    @Column(name = "version")
    private int version;

    @Column
    @NotNull
    private UserProjects userProject;

    @Column
    @NotNull
    private Timestamp start;

    @Column
    @NotNull
    private Timestamp end;

}

I omitted all getters and setters. The problem is on peristing booking i get a strange exception: 
Value too long for column "USERPROJECT BINARY(255) NOT NULL": "X'aced00057372002d636f6d2e70726f64796e612e7061632e74696d747261636b65722e6d6f64656c2e5573657250726f6a6563747300000000000000010200... (572)"; SQL statement:
insert into Booking (end, start, userProject, version, id) values (?, ?, ?, ?, ?) [22001-173]

So for some reason jpa tries to store UserProject as binary and not as a foreign key from UserProject table. So how to fix it, how to correctly design the jpa classes for this use case?

A:

Your relationship to UserProjects isn't defined as a foreign key.  You need to use @ManyToOne (i'm assuming many bookings may refer to one UserProjects instance?) as well as @JoinColumn (instead of your @Column) in order to trigger a foreign key.  What I believe your seeing is the serialized value trying to be inserted.

