Hey people,
I went through the Hibernate for Netbeans 6.8 tutorial which utilized the example database Sakila to create a basic JSF and Hibernate web app. Everything worked fine for the tutorial, but when I came to following the same steps for my own database I get stuck on using the HQL query.
Basically the tutorial did this:
1) Created Project
2) Configured hibernate.cfg.xml
3) Created the HibernateUtil.java helper file
4) Created a hibernate reverse engineering file
5) Created the Hibernate Mapping Files and POJOs
6) Right clicked hibernate.cfg.xml --> Run HQL Query
When I get to step 6 using my own database (which is a MySQL InnoDB with foreign keys just like the example) I get the following error when I execute 'from Hosts':
Code:
org.hibernate.MappingException: An association from the table hosts_payments refers to an unmapped class: com.bleh.Payments
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1252)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1170)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
The error only occurs when I introduce link tables into the equation. The thing is, the example database used link tables aswell - and the Mapping Files and Pojos wizard created .java and .hbm.xml for the tutorials link tables, and uses <one-to-many> in the mapping file. When it generates mine though, it doesn't create anything for the link tables and uses <many-to-many> in my mapping file.
Can somebody please point out where I'm going wrong? I only tuned into Hibernate this morning so it's just a bit confusing at the minute!
The link table is called hosts_payments --- but as I mentioned, nothing was generated for this.
The following are the generated files (i omitted the options.java and the options.hbm.xml as they appear to be working):
hibernate.cfg.xmlCode:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/database</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">********</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/bleh/Options.hbm.xml"/>
<mapping resource="com/bleh/Hosts.hbm.xml"/>
<mapping resource="com/bleh/Payments.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hosts.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 14-Apr-2010 18:42:55 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.bleh.Hosts" table="hosts" catalog="lagless_frag_development">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="optionsByRefSys" class="com.bleh.Options" fetch="select">
<column name="ref_sys" />
</many-to-one>
<many-to-one name="optionsByDonSys" class="com.bleh.Options" fetch="select">
<column name="don_sys" />
</many-to-one>
<many-to-one name="optionsByDedServer" class="com.bleh.Options" fetch="select">
<column name="ded_server" not-null="true" />
</many-to-one>
<property name="title" type="string">
<column name="title" not-null="true" unique="true" />
</property>
<property name="referralUrl" type="string">
<column name="referral_url" length="20" />
</property>
<property name="owner" type="java.lang.Integer">
<column name="owner" />
</property>
<property name="referralClicks" type="java.lang.Integer">
<column name="referral_clicks" />
</property>
<property name="createdAt" type="timestamp">
<column name="created_at" length="19" not-null="true" />
</property>
<property name="updatedAt" type="timestamp">
<column name="updated_at" length="19" />
</property>
<set name="paymentses" inverse="false" table="hosts_payments">
<key>
<column name="host_id" not-null="true" />
</key>
<many-to-many entity-name="com.bleh.Payments">
<column name="payment_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
Hosts.javaCode:
package com.bleh;
// Generated 14-Apr-2010 18:42:55 by Hibernate Tools 3.2.1.GA
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
/**
* Hosts generated by hbm2java
*/
public class Hosts implements java.io.Serializable {
private Integer id;
private Options optionsByRefSys;
private Options optionsByDonSys;
private Options optionsByDedServer;
private String title;
private String referralUrl;
private Integer owner;
private Integer referralClicks;
private Date createdAt;
private Date updatedAt;
private Set<Payments> paymentses = new HashSet<Payments>(0);
public Hosts() {
}
public Hosts(Options optionsByDedServer, String title, Date createdAt) {
this.optionsByDedServer = optionsByDedServer;
this.title = title;
this.createdAt = createdAt;
}
public Hosts(Options optionsByRefSys, Options optionsByDonSys, Options optionsByDedServer, String title, String referralUrl, Integer owner, Integer referralClicks, Date createdAt, Date updatedAt, Set<Payments> paymentses) {
this.optionsByRefSys = optionsByRefSys;
this.optionsByDonSys = optionsByDonSys;
this.optionsByDedServer = optionsByDedServer;
this.title = title;
this.referralUrl = referralUrl;
this.owner = owner;
this.referralClicks = referralClicks;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.paymentses = paymentses;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Options getOptionsByRefSys() {
return this.optionsByRefSys;
}
public void setOptionsByRefSys(Options optionsByRefSys) {
this.optionsByRefSys = optionsByRefSys;
}
public Options getOptionsByDonSys() {
return this.optionsByDonSys;
}
public void setOptionsByDonSys(Options optionsByDonSys) {
this.optionsByDonSys = optionsByDonSys;
}
public Options getOptionsByDedServer() {
return this.optionsByDedServer;
}
public void setOptionsByDedServer(Options optionsByDedServer) {
this.optionsByDedServer = optionsByDedServer;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getReferralUrl() {
return this.referralUrl;
}
public void setReferralUrl(String referralUrl) {
this.referralUrl = referralUrl;
}
public Integer getOwner() {
return this.owner;
}
public void setOwner(Integer owner) {
this.owner = owner;
}
public Integer getReferralClicks() {
return this.referralClicks;
}
public void setReferralClicks(Integer referralClicks) {
this.referralClicks = referralClicks;
}
public Date getCreatedAt() {
return this.createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return this.updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
public Set<Payments> getPaymentses() {
return this.paymentses;
}
public void setPaymentses(Set<Payments> paymentses) {
this.paymentses = paymentses;
}
}
HostsHelper.javaCode:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.bleh;
import org.hibernate.Session;
/**
*
* @author Administrator
*/
public class HostHelper {
Session session = null;
public HostHelper() {
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
}
}
payments.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 14-Apr-2010 18:42:55 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.bleh.Payments" table="payments" catalog="lagless_frag_development">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="title" type="string">
<column name="title" length="100" not-null="true" />
</property>
<property name="image" type="string">
<column name="image" length="100" />
</property>
<set name="hostses" inverse="false" table="hosts_payments">
<key>
<column name="payment_id" not-null="true" />
</key>
<many-to-many entity-name="com.bleh.Hosts">
<column name="host_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
Payments.javaCode:
package com.bleh;
// Generated 14-Apr-2010 18:42:55 by Hibernate Tools 3.2.1.GA
import java.util.HashSet;
import java.util.Set;
/**
* Payments generated by hbm2java
*/
public class Payments implements java.io.Serializable {
private Integer id;
private String title;
private String image;
private Set<Hosts> hostses = new HashSet<Hosts>(0);
public Payments() {
}
public Payments(String title) {
this.title = title;
}
public Payments(String title, String image, Set<Hosts> hostses) {
this.title = title;
this.image = image;
this.hostses = hostses;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return this.image;
}
public void setImage(String image) {
this.image = image;
}
public Set<Hosts> getHostses() {
return this.hostses;
}
public void setHostses(Set<Hosts> hostses) {
this.hostses = hostses;
}
}
Any info appreciated thanks..