Hibernate:一对多和继承,表没有被创建

时间:2014-09-16 08:29:49

标签: java hibernate

我在两个类之间建立了一对多的关系芯片(实际上有效)。但是,只要我添加第二个类,它扩展了其中一个类,就不再创建表了。我无法弄清楚原因。

不久: A类与C类有1对多的关系, B类扩展了A类

@Entity
@Table(name = "PRODUCT")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator",discriminatorType=DiscriminatorType.STRING )
@DiscriminatorValue(value="P")
public class Product{

@Id
@GeneratedValue
@Column(name = "PRODUCT_ID")
private int productID;

@Column(name = "ARTICLE_NUMBER")
private String articleNumber;

@Column(name = "DESCRIPTION")
private String description;

@Column(name = "TITLE")

@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name="MANUFACTURER_TITLE")
private Manufacturer manufacturer;

//Constructor, getter and setter
}

子类:

@Entity
@Table(name="PRODUCT")
@DiscriminatorValue("E")
public class Service extends Product{

@Column(name = "INTERVAL")
private int interval;

@Column(name = "UNITS")
private String units;
//Constructor, getter and setter
}

Relationchip to:

@Entity
@Table(name = "MANUFACTURER")
public class Manufacturer implements Serializable {

@Id
@Column(name = "MANUFACTURER_TITLE")
private String title;

@OneToMany(mappedBy="manufacturer")
private Set<Product> products;
//Constructor, getter and setter
}

映射:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <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/testshop</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"/>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">1500</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <!--Autocreate Tables-->
    <!--property name="hibernate.archive.autodetection" value="class"/-->
    <property name="hibernate.hbm2ddl.auto">create</property>
    <!--Debug show sql-->
    <property name="show_sql">true</property>
    <!--Character Encoding for Using UTF-8 to use german special characters-->
    <property name="hibernate.connection.CharSet">utf8</property>
    <property name="hibernate.connection.characterEncoding">utf8</property>
    <property name="hibernate.connection.useUnicode">true</property>
    <!--Entity to be mapped-->
    <!--Caching for performance-->
    <property name="hibernate.cache.provider_class">
      org.hibernate.cache.EhCacheProvider
   </property>
    <mapping class="test.Department"/>
    <mapping class="test.Person"/>
    <mapping class="test.Employee"/>
    <mapping class="main.ShopConfig"/>
    <mapping class="main.Logging"/>
    <mapping class="product.Product"/>
    <mapping class="product.Service"/>
    <mapping class="manufacturer.Manufacturer"/>

  </session-factory>
</hibernate-configuration>

我添加服务类的映射后,它就不再起作用..

编辑: 如果我删除了一对多关系芯片,表格就会被创建..但为什么?!

EDIT2: 休眠错误:

    16-09-2014 10:55:14 ERROR SchemaExport:425 - HHH000389: Unsuccessful: create table PRODUCT      (discriminator varchar(31) not null, PRODUCT_ID integer not null auto_increment, ARTICLE_NUMBER         varchar(255), DESCRIPTION varchar(255), END_DATE tinyblob, PRICE double precision, PUBLISHED bit,       QUANTITY integer, START_DATE tinyblob, TITLE varchar(255), UNLIMITED bit, WEIGHT varchar(255),      INTERVAL integer, UNITS varchar(255), MANUFACTURER_TITLE varchar(255), primary key (PRODUCT_ID))
    10705 [http-bio-8080-exec-12] ERROR org.hibernate.tool.hbm2ddl.SchemaExport  - HHH000389:       Unsuccessful: create table PRODUCT (discriminator varchar(31) not null, PRODUCT_ID integer not null         auto_increment, ARTICLE_NUMBER varchar(255), DESCRIPTION varchar(255), END_DATE tinyblob, PRICE         double precision, PUBLISHED bit, QUANTITY integer, START_DATE tinyblob, TITLE varchar(255), UNLIMITED       bit, WEIGHT varchar(255), INTERVAL integer, UNITS varchar(255), MANUFACTURER_TITLE varchar(255),        primary key (PRODUCT_ID))

    16-09-2014 10:55:14 ERROR SchemaExport:426 - You have an error in your SQL syntax; check the                        manual that corresponds to your MySQL server version for the right syntax to use near 'INTERVAL         integer, UNITS varchar(255), MANUFACTURER_TITLE varchar(255), primary k' at line 1
    10706 [http-bio-8080-exec-12] ERROR org.hibernate.tool.hbm2ddl.SchemaExport  - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the         right syntax to use near 'INTERVAL integer, UNITS varchar(255), MANUFACTURER_TITLE varchar(255),        primary k' at line 1

1 个答案:

答案 0 :(得分:1)

问题在于字段'interval'。它是MySql中的保留字。将其更改为其他内容。