Being new to ORM, I'd like to find a way to define a simple (meaning without an additional entity) mapping for a list (or a set) of strings within an entity. I found this sample:
import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue
private Long id;
@ElementCollection
@CollectionTable(name = "tags")
private Set<String> tags;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<String> getTags() {
return tags;
}
public void setTags(Set<String> tags) {
this.tags = tags;
}
}
which seems to fit my needs. However, processing this class with Eclipse's hibernate3-maven-plugin:2.2:hbm2ddl
, I end up with the following error:
[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2ddl (default) on project test-database: Execution default of goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2ddl failed: Could not determine type for: java.util.Set, at table: Book, for columns: [org.hibernate.mapping.Column(tags)] -> [Help 1]
Specifying @ElementCollection(targetClass=String.class)
did not help. Adding a column definition to the tags
field (@Column(name = "tags", columnDefinition="character varying (255)", nullable = false)
) leads to a successful build but produces this SQL:
create table Book (
id int8 not null,
tags character varying (255) not null,
primary key (id)
);
which is not what I want, as I was expecting to end up with a tags
table linked to the books table. Could someone point me to the right direction ? Thank you.
@ElementCollection has been introduced in JPA v 2.0: the mapping you've done is correct. However make sure the maven hibernate plugin you use is at the correct version. Hibernate itself is compliant with JPA 2.0 starting at version 3.5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With