Hibernate много-ко-многим + таблицы дополнительных свойств - PullRequest
0 голосов
/ 09 февраля 2012

У меня есть сервлет, который подключается к базе данных MySQL через Hibernate.

И я получаю следующие исключения:

<code>
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'berelhetoe0_.pk.terem' in 'field list'
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
        at com.mysql.jdbc.Util.getInstance(Util.java:386)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2293)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
        ... 33 more

Вот как построен мой БД:

 
    DROP TABLE IF EXISTS <code>berelheto</code>;
    CREATE TABLE <code>berelheto</code> (
      <code>TeremID</code> int(11) NOT NULL,
      <code>EszkozID</code> int(11) NOT NULL,
      <code>Available</code> int(11) DEFAULT NULL,
      PRIMARY KEY (<code>TeremID</code>,<code>EszkozID</code>)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;</p>

<pre><code>DROP TABLE IF EXISTS `eszkozok`;
CREATE TABLE `eszkozok` (
  `EszkozID` int(11) NOT NULL AUTO_INCREMENT,
  `EszkozNev` varchar(1000) NOT NULL,
  UNIQUE KEY `EszkozID` (`EszkozID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;


DROP TABLE IF EXISTS `termek`;

CREATE TABLE `termek` (
  `TeremID` int(11) NOT NULL AUTO_INCREMENT,
  `TeremNev` varchar(1000) DEFAULT NULL,
  `Path` varchar(1000) DEFAULT NULL,
  `HetkozFrom` time DEFAULT NULL,
  `HetkozTo` time DEFAULT NULL,
  `SzombatFrom` time DEFAULT NULL,
  `SzombatTo` time DEFAULT NULL,
  `VasarnapFrom` time DEFAULT NULL,
  `VasarnapTo` time DEFAULT NULL,
  UNIQUE KEY `TeremID` (`TeremID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

Мои xml-файлы:

Terem.hbm.xml:

 
    </p>

<pre><code><!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="beans.Terem" table="termek">
        <id name="TeremID">
            <generator class="native"/>
        </id>
        <property name="TeremNev" />
        <property name="Path" />
        <property name="HetkozFrom" />
        <property name="HetkozTo" />
        <property name="SzombatFrom" />
        <property name="SzombatTo" />
        <property name="VasarnapFrom" />
        <property name="VasarnapTo" />

        <set name="Foglalasok" table="foglalt">
            <key>
                <column name="TeremID" not-null="true" />
            </key>
            <one-to-many class="beans.Foglalas" />
        </set>

        <set name="Tipusok" table="milyen">
            <key column="TeremID" />
            <many-to-many column="TeremTipusID" class="beans.TeremTipus" />
        </set>

        <set name="BerelhetoEszkozok" table="berelheto" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="pk.terem" />
            </key>
            <one-to-many class="beans.Berelheto" />
        </set>
    </class>
</hibernate-mapping>

Eszkoz.hbm.xml:

 
    </p>

<pre><code><!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="beans.Eszkoz" table="eszkozok">
        <id name="EszkozID">
            <generator class="native"/>
        </id>
        <property name="EszkozNev" />

        <set name="Termeim" table="berelheto" >
            <key> 
                <column name="pk.eszkoz" not-null="true" />
            </key>
            <one-to-many class="beans.Berelheto"/>
         </set>
    </class>
</hibernate-mapping>

Berelheto.hbm.xml:

 
    </p>

<pre><code><!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="beans.Berelheto" table="berelheto">

    <composite-id name="pk" class="beans.BerelhetoID">

        <key-property name="terem" column="terem" />
        <key-property name="eszkoz" column="eszkoz" />

    </composite-id>

    <component name="terem">
        <many-to-one name="TeremID" class="beans.Terem" update="false" insert="false">
            <column name="TeremID" not-null="true"/>
        </many-to-one>
    </component>

    <component name="eszkoz">
        <many-to-one name="EszkozID" class="beans.Eszkoz" update="false" insert="false">
            <column name="EszkozID" not-null="true"/>
        </many-to-one>
    </component>

    <property name="Available"/>

    </class>
</hibernate-mapping>

И, наконец, мои классы Java:

Terem.java:

 
    package beans;</p>

<pre><code>import java.sql.Time;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import static javax.persistence.GenerationType.IDENTITY;

public class Terem implements java.io.Serializable{

    private static final long serialVersionUID = 1L;
    private int TeremID;
    private String TeremNev;
    private String Path;
    private Time HetkozFrom;
    private Time HetkozTo;
    private Time SzombatFrom;
    private Time SzombatTo;
    private Time VasarnapFrom;
    private Time VasarnapTo;

    private Set<Foglalas> Foglalasok = new HashSet<Foglalas>(0);

    private Set<TeremTipus> Tipusok = new HashSet<TeremTipus>(0);

    private Set<Berelheto> BerelhetoEszkozok = new HashSet<Berelheto>(0);

    public Terem() {

    }
    ...

Eszkoz.java:

 
    package beans;</p>

<pre><code>import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

public class Eszkoz implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private int EszkozID;
    private String EszkozNev;
    private Set<Berelheto> Termeim = new HashSet<Berelheto>(0);

    public Eszkoz() {

    }

    public Eszkoz(String EszkozNev) {
        this.EszkozNev = EszkozNev;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "EszkozID", unique = true, nullable = false)
    public int getEszkozID() {
        return EszkozID;
    }
        ....

Berelheto.java:

 
    package beans;</p>

<pre><code>import java.util.HashSet;
import java.util.Set;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;


@Entity
@Table(name = "berelheto", catalog="sport")
@AssociationOverrides({
        @AssociationOverride(name = "pk.terem", joinColumns = @JoinColumn(name = "TeremID")),
        @AssociationOverride(name = "pk.eszkoz", joinColumns = @JoinColumn(name = "EszkozID")) })

public class Berelheto implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private BerelhetoID pk = new BerelhetoID();
    private int Available;
    private Set<Berelheto> Termeim = new HashSet<Berelheto>(0);

    @EmbeddedId
    public BerelhetoID getPk() {
        return pk;
    }

    public void setTerem(Terem t){
        getPk().setTerem(t);
    }

    @Transient
    public Terem getTerem() {
        return getPk().getTerem();
    }
    public void setEszkoz(Eszkoz e) {
        getPk().setEszkoz(e);
    }

    @Transient
    public Eszkoz getEszkoz() {
        return getPk().getEszkoz();
    }
        ...

BerelhetoID.java:

 
    package beans;</p>

<pre><code>import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;

@Embeddable
public class BerelhetoID implements java.io.Serializable{

    private static final long serialVersionUID = 1L;
    private Terem terem;
    private Eszkoz eszkoz;


    public BerelhetoID() {

    }

    @ManyToOne
    public Terem getTerem() {
        return terem;
    }
    public void setTerem(Terem terem) {
        this.terem = terem;
    }
    @ManyToOne
    public Eszkoz getEszkoz() {
        return eszkoz;
...

Любые замечания, исправления приветствуются!

1 Ответ

0 голосов
/ 09 февраля 2012

Я не очень опытен в спящем режиме. Но когда он не может найти столбец, я искал ошибку в Berelheto.java И попробуйте что-то вроде:

@Table(name = "berelheto", catalog="sport")
@AssociationOverrides({
        @AssociationOverride(name = "TeremID", joinColumns = @JoinColumn(name = "TeremID")),
        @AssociationOverride(name = "pk.eszkoz", joinColumns = @JoinColumn(name = "EszkozID")) })
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...