Hi everyone,
first I want you to excuse me for my English but I'm German and I will try to explain my issue as well as I can. I'm very new in working with Hibernate and in the last two days I have been struggling to solve a problem, actually to get rid of an exception (a mapping seems to be the problem). Here's the exception I get in NetBeans:
"Initial SessionFactory creation failed.org.hibernate.MappingException:
Foreign key (id:GERICHTHATPLUS [id])) must have same number of columns as the referenced primary key (BESTELLUNGGERICHTTABLE [bnr,gnr])"
So, it's very clear to me that the mapping is not completely correct. Where the problem is, I cannot tell. I lost my last two days trying to get it work, but unfortunately it won't.
I tried searching on Google for solutions, unfortunately without success. My last chance is this forum. So, here are the details: I have this homework where I must create a Pizzeria Order-system. I generated the Java classes using JPA, then I wrote the mapping files by hand. 90 % of them work (I tested them by making some simple selects and printing out the results), but the last relation I mapped (a many-to-many relation), won't give me peace...
Here is my DB-structure (sorry for the german names):
-
Gerichttable (Gnr, Name, Grundpreis, Art), where Gnr is the PRIMARY KEY
-
Kundetable (Knr, Name, Anschrift, Telefon), Knr - PRIMARY KEY
-
Plustable (Pnr, Name, Einzelpreis, passtzu), Pnr - PRIMARY KEY
-
Kundebestellungtable (Bnr, datum, Knr), Bnr - PRIMARY KEY, Knr is the FOREIGN KEY from Kundetable
-
Bestellunggerichttable (Id, Bnr, Gnr, Anzahl), Id - PRIMARY KEY, FOREIGN KEY are Bnr and Gnr
-
GerichtHatPlus_table (Id, Pnr) - PRIMARY KEY is (Id, Pnr), both are FOREIGN KEYS
Why this complicated structure? Well, here are some extra details about my project: there should be two types of dishes - pizzas and salads. To a pizza you can add (beside the standard ingredients), extras (which can be, for eg. extra cheese, extra olives etc.). To a salad you can add dressings (mustard-dressing etc.). But: you cannot add a dressing to a pizza or an extra to a salad (I think it's obvious).
Other important things for you to know
-
GerichtHatPlus_table is a join table between Plustable and Bestellunggerichttable.
- Between
Kundebestellungtable and
Gerichttable there is a M:N relation, which I "solved" with an extra table:
Bestellunggerichttable, which has two additional columns (maybe this is the thing which generates my error, because I don't know how to make the mapping in this case) - Id and Anzahl, where Id is the PRIMARY KEY; Bnr and Gnr are here FOREIGN KEYS from the other two tables.
Back to my problem: I give you here my mappings which are not correct and part of the corresponding java classes: Plustable -
plus.hbm.xml and Bestellunggerichttable -
bestellunggericht.hbm.xml (all other mappings do work, so we don't need to bother about them here):
plus.hbm.xmlCode:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Plus" table="PLUSTABLE">
<id name="pnr" type="big_decimal">
<column name="PNR"/>
<generator class="identity"/>
</id>
<property name="name" type="string">
<column length="80" name="name"/>
</property>
<property name="einzelpreis" type="big_decimal">
<column length="20" name="einzelpreis" not-null="true"/>
</property>
<property name="passtzu" type="string">
<column length="10" name="passtzu" not-null="true"/>
</property>
<set name="bestellunggerichten" table="GERICHTHATPLUS">
<key column="pnr"/>
<many-to-many class="entity.Bestellunggericht" column="id"/>
</set>
</class>
</hibernate-mapping>
Plus.javaCode:
@Entity
@Table(name = "PLUSTABLE")
@NamedQueries({@NamedQuery(name = "Plus.findAll", query = "SELECT p FROM Plus p"), @NamedQuery(name = "Plus.findByPnr", query = "SELECT p FROM Plus p WHERE p.pnr = :pnr"), @NamedQuery(name = "Plus.findByName", query = "SELECT p FROM Plus p WHERE p.name = :name"), @NamedQuery(name = "Plus.findByEinzelpreis", query = "SELECT p FROM Plus p WHERE p.einzelpreis = :einzelpreis"), @NamedQuery(name = "Plus.findByPasstzu", query = "SELECT p FROM Plus p WHERE p.passtzu = :passtzu")})
public class Plus implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "PNR")
private BigDecimal pnr;
@Column(name = "NAME")
private String name;
@Column(name = "EINZELPREIS")
private BigDecimal einzelpreis;
@Column(name = "PASSTZU")
private String passtzu;
@ManyToMany(mappedBy = "plusCollection")
private Set<Bestellunggericht> bestellunggerichten;
...and all other get() and set() methods
bestellunggericht.hbm.xmlCode:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Bestellunggericht" table="BESTELLUNGGERICHTTABLE">
<id name="id" type="integer">
<column name="id"/>
<generator class="identity"/>
</id>
<property name="anzahl" type="integer">
<column name="anzahl" not-null="true"/>
</property>
<set name="plusen" table="GERICHTHATPLUS">
<key column="id"/>
<many-to-many class="entity.Plus" column="pnr"/>
</set>
</class>
</hibernate-mapping>
Bestellunggericht.javaCode:
@Entity
@Table(name = "BESTELLUNGGERICHTTABLE")
@NamedQueries({@NamedQuery(name = "Bestellunggericht.findAll", query = "SELECT b FROM Bestellunggericht b"), @NamedQuery(name = "Bestellunggericht.findById", query = "SELECT b FROM Bestellunggericht b WHERE b.id = :id"), @NamedQuery(name = "Bestellunggericht.findByAnzahl", query = "SELECT b FROM Bestellunggericht b WHERE b.anzahl = :anzahl")})
public class Bestellunggericht implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private BigDecimal id;
@Basic(optional = false)
@Column(name = "ANZAHL")
private BigInteger anzahl;
@JoinTable(name = "GERICHTHATPLUS", joinColumns = {@JoinColumn(name = "ID", referencedColumnName = "ID")}, inverseJoinColumns = {@JoinColumn(name = "PNR", referencedColumnName = "PNR")})
@ManyToMany
private Set<Plus> plusen;
@JoinColumn(name = "GNR", referencedColumnName = "GNR")
@ManyToOne
private Gericht gnr;
@JoinColumn(name = "BNR", referencedColumnName = "BNR")
@ManyToOne
private Kundebestellung bnr;
...and all other get() and set() methods
Not to forget, I've automatically generated the entities with JPA (in NetBeans, where I developed my project), and I just modified the code where it was necessary.
Could please someone help me get rid of this error and understand what is happening? I'm going crazy...
Any help would be kindly appreciated.
Thanks in advance.