My mappings are somewhat different from the original poster's, but the problem is the same. I'm mapping a collectionOfElements without the ManyToMany annotation, and using a composite key (couldn't think of another way... not happy with that part). The inverseJoinColumn is for specifying the mapping to the Ad table. Anyhow, the uniqueness constraint causing the problem in my case is on publisher_channel_opt_in.ad_id:
The entity class...
Code:
@Entity
@Table(name = "publisher_channel")
public class PublisherChannel {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "publisherId",
column = @Column(name = "publisher_id")),
@AttributeOverride(name = "channel",
column = @Column(name = "channel"))
})
private PublisherChannelId id;
@CollectionOfElements
@JoinTable(
name = "publisher_channel_opt_in",
joinColumns = {
@JoinColumn(name = "channel", unique = false, nullable = false),
@JoinColumn(name = "publisher_id", unique = false, nullable = false)
},
inverseJoinColumns = @JoinColumn(name = "ad_id", unique = false, nullable = false)
)
@IndexColumn(name = "position", base = 1)
private List<Ad> publisherChannelOptIn;
...
And the composite key class...
Code:
public class PublisherChannelId implements Serializable {
private static final long serialVersionUID = 1L;
private String publisherId;
private String channel;
public PublisherChannelId() {
}
public PublisherChannelId(String publisherId, String channel) {
this.publisherId = publisherId;
this.channel = channel;
}
...
The tables this creates look like this (the UNI constraint on ad_id is the problem):
Code:
mysql> describe publisher_channel_opt_in;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| channel | varchar(255) | NO | PRI | | |
| publisher_id | varchar(255) | NO | PRI | | |
| ad_id | int(11) | NO | UNI | | |
| position | int(11) | NO | PRI | | |
+--------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> describe publisher_channel;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| channel | varchar(255) | NO | PRI | | |
| publisher_id | varchar(255) | NO | PRI | | |
+--------------+--------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
btw, the thing I'm trying to accomplish is making this bit of currently hard-coded data persistent. I'm still learning hibernate, so if I'm doing things the hard way out of ignorance (likely) I'd appreciate a helping hand:
Code:
final Map<String, List<ChannelAds>> targetedChannels;
public static class ChannelAds {
private String channel;
private List<Integer> ads;
public ChannelAds(String channel, Integer... ads) {
this.channel = channel;
this.ads = Arrays.asList(ads);
}
}