JPA How add unique contraint on column for @OneToMany relation like on username

Let the user have a Site reference:

@ManyToOne(optional=false)
private Site site;

Now add the constraint to user:

@Table(uniqueConstraints = {
    @UniqueConstraint(columnNames = { "username", "site" })})
@Entity
public class User{
// etc
}

You will also have to change the Site mapping:

@OneToMany(mappedBy="site")
private List<User> users;

By default, the primary index on the join table is unique and based on the site and user FK So, you cannot have the same user with the same site duplicated.

But if you want to force a constraint :

class Site {

    private int site_ID;

    @OneToMany // with a join table
    @JoinTable(
        uniqueConstraints=@UniqueConstraint(columnNames={"Site_ID","users_ID"})
    )
    private List<User> users;
    // ...
}