Directly. I use Hibernate 3.2 and hibernate-annotations 3.2.0CR2. I have three tables:
Compu:
compid: int
name: varchar
description: varchar
ipaddress: varchar
Compugr:
compugrid: int
name: varchar
description: varchar
Compugrmem:
compugrmemid: int
compuid: int
compugrid: int
A computer belongs to zero or more groups and a group contains zero or more computers. I use annotations:
HibernateUtil.java
Code:
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static void initialize(String hibernateConfigFile) {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration().configure(new File(hibernateConfigFile)).buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Base.java
Code:
@MappedSuperclass
public class Base {
private static Logger log = Logger.getLogger(Base.class);
@Id()
private int id;
private String name;
private String decription;
protected Base() {
log.debug("Creando nuevo objeto.");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDecription() {
return decription;
}
public void setDecription(String decription) {
this.decription = decription;
}
@Transient
public void save() {
log.debug("Guardando el objeto...");
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
session.save(this);
session.getTransaction().commit();
log.debug("Objeto guardado.");
}
}
Computer.java
Code:
@Entity()
@Table(name="COMPU")
@AttributeOverride( name="id", column = @Column(name="COMPID") )
public class Computer extends Base {
private Collection<ComputerGroup> computerGroupList;
private String ipAddress;
public Computer() {
}
@Transient
@ManyToMany(
cascade={CascadeType.PERSIST, CascadeType.MERGE},
mappedBy="computersList",
targetEntity=ComputerGroup.class
)
public Collection<ComputerGroup> getComputerGroupList() {
return computerGroupList;
}
public void setComputerGroupList(Collection<ComputerGroup> computerGroupList) {
this.computerGroupList = computerGroupList;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
}
ComputerGroup.java
Code:
@Entity()
@Table(name="COMPU")
@AttributeOverride( name="id", column = @Column(name="COMPID") )
public class Computer extends Base {
private Collection<ComputerGroup> computerGroupList;
private String ipAddress;
public Computer() {
}
@Transient
@ManyToMany(
cascade={CascadeType.PERSIST, CascadeType.MERGE},
mappedBy="computersList",
targetEntity=ComputerGroup.class
)
public Collection<ComputerGroup> getComputerGroupList() {
return computerGroupList;
}
public void setComputerGroupList(Collection<ComputerGroup> computerGroupList) {
this.computerGroupList = computerGroupList;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
}
When the servlet starts, I get this error:
Code:
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: java.util.Collection, for columns: [org.hibernate.mapping.Column(computerGroupList)]
Any idea?