错误的SchemaExport与Hibernate

时间:2014-07-09 12:52:36

标签: java mysql hibernate runtime-error

我想用 Hibernate 和MySql创建embedded个对象的列表。

但是我有很多错误:

Hibernate: alter table USERS drop foreign key FK_qymdwjo8d0eu0lhfd3ngfs74d
2014-07-09 15:40:47 ERROR SchemaExport:425 - HHH000389: Unsuccessful: alter table USERS drop foreign key FK_qymdwjo8d0eu0lhfd3ngfs74d
2014-07-09 15:40:47 ERROR SchemaExport:426 - Can't DROP 'FK_qymdwjo8d0eu0lhfd3ngfs74d'; check that column/key exists
Hibernate: drop table if exists USERS
2014-07-09 15:40:48 ERROR SchemaExport:425 - HHH000389: Unsuccessful: drop table if exists USERS
2014-07-09 15:40:48 ERROR SchemaExport:426 - Cannot delete or update a parent row: a foreign key constraint fails
Hibernate: drop table if exists hibernate_unique_key
Hibernate: create table USERS (id integer not null, city varchar(35), pincode varchar(35), state varchar(35), street varchar(35), description varchar(35), joinedDate date, name varchar(35), ADDRESSES_ID bigint not null, primary key (ADDRESSES_ID))
2014-07-09 15:40:48 ERROR SchemaExport:425 - HHH000389: Unsuccessful: create table USERS (id integer not null, city varchar(35), pincode varchar(35), state varchar(35), street varchar(35), description varchar(35), joinedDate date, name varchar(35), ADDRESSES_ID bigint not null, primary key (ADDRESSES_ID))
2014-07-09 15:40:48 ERROR SchemaExport:426 - Table 'users' already exists
Hibernate: alter table USERS add constraint FK_qymdwjo8d0eu0lhfd3ngfs74d foreign key (id) references USERS (ADDRESSES_ID)
2014-07-09 15:40:48 ERROR SchemaExport:425 - HHH000389: Unsuccessful: alter table USERS add constraint FK_qymdwjo8d0eu0lhfd3ngfs74d foreign key (id) references USERS (ADDRESSES_ID)
2014-07-09 15:40:48 ERROR SchemaExport:426 - Cannot add foreign key constraint
Hibernate: create table hibernate_unique_key ( next_hi integer )
Hibernate: insert into hibernate_unique_key values ( 0 )
2014-07-09 15:40:48 INFO  SchemaExport:405 - HHH000230: Schema export complete
Hibernate: insert into USERS (city, pincode, state, street, description, joinedDate, name) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select next_hi from hibernate_unique_key for update
Hibernate: update hibernate_unique_key set next_hi = ? where next_hi = ?
Hibernate: insert into USERS (id, ADDRESSES_ID, city, pincode, state, street) values (?, ?, ?, ?, ?, ?)
2014-07-09 15:40:48 WARN  SqlExceptionHelper:144 - SQL Error: 1054, SQLState: 42S22
2014-07-09 15:40:48 ERROR SqlExceptionHelper:146 - Unknown column 'ADDRESSES_ID' in 'field list'
2014-07-09 15:40:48 INFO  AbstractBatchImpl:208 - HHH000010: On release of batch it still contained JDBC statements
org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
    at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1311)
    at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:67)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at com.demo.hibernate.HibernateDemo.createUser(HibernateDemo.java:59)
    at com.demo.hibernate.HibernateDemo.main(HibernateDemo.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'ADDRESSES_ID' in 'field list'

这是 main()

public static void main(String[] args) {
    try {
        HibernateDemo demo = new HibernateDemo();

        UserDetails user = new UserDetails();

        Address address = new Address();
        address.setStreet("Name");
        address.setCity("Kiev");
        address.setPincode("00000");
        address.setState("My state");

        Address addr = new Address();
        addr.setStreet("new name");
        addr.setCity("Lviv");
        addr.setPincode("79040");
        addr.setState("state");

        user.getListOfAddresses().add(address);
        user.getListOfAddresses().add(addr);
        user.setUserName("Carl");
        user.setJoinedDate(new Date());
        user.setDescription("it is cool guy");
        user.setAddress(address);

        demo.createUser(user);
        demo.listUsers();
        user.setUserName("Bruno Shults");
        demo.updateUser(user);

        demo.listUsers();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        System.runFinalizersOnExit(true);
        System.exit(1);
    }

UserDetails 类:

@Entity
@Table(name = "USERS")
public class UserDetails {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int userId;

    @Column(name = "name", length = 35)
    private String userName;

    @Temporal(TemporalType.DATE)
    private Date joinedDate;

    @Column(length = 35)
    private Address address;

    @Column(length = 35)
    private String description;

    @ElementCollection
    @JoinTable(name = "USERS", joinColumns = @JoinColumn(name = "id"))
    @GenericGenerator(name = "hilo-gen", strategy = "hilo")
    @CollectionId(columns = {@Column(name = "ADDRESSES_ID")}, generator = "hilo-gen", type = @Type(type = "long"))
    private Collection<Address> listOfAddresses = new ArrayList<Address>();
    // getters and setters

地址 类:

@Embeddable
public class Address {
    @Column(length = 35)
    private String street;

    @Column(length = 35)
    private String city;

    @Column(length = 35)
    private String state;

    @Column(length = 35)
    private String pincode;
    // getters and setters

cfg xml 文件:

<hibernate-configuration>
    <session-factory>
        <!--Database connection settings-->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatedb</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">secret</property>

        <!--JDBC connection pool-->
        <property name="hibernate.connection.pool_size">2</property>

        <!--SQL dialect-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!--Disable the second level cache-->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>

        <!--Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!--Drop and recreate the database schema on startup-->
        <property name="hbm2ddl.auto">create</property>
        <!--<property name="hbm2ddl.auto">update</property>-->

        <!-- List of XML mapping files -->
        <mapping class="com.demo.dto.UserDetails"/>

这是数据库的结果:

enter image description here

和图表:

enter image description here

我无法弄清楚它为什么会创建hibernate_unique_key表?它不应该。

有任何建议吗?

1 个答案:

答案 0 :(得分:1)

这些行

Hibernate: create table hibernate_unique_key ( next_hi integer )
Hibernate: insert into hibernate_unique_key values ( 0 )

出现因为

@GenericGenerator(name = "hilo-gen", strategy = "hilo")

在您的地址

hilo:生成整数,长或短类型的id。这使用高 - 低算法。顾名思义,它取决于最高表id,然后读取可能的最低可用值。

详细了解here

相关问题