所以我正在尝试创建一个名为Author
的类和一个名为Book
的类。每个Book
都与Author
相关联。所以我有下面的表格模式:
CREATE TABLE author (
author_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
email VARCHAR(50),
PRIMARY KEY(author_id)
);
和
CREATE TABLE book (
book_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(30),
description VARCHAR(50),
publish_date DATE,
author_id BIGINT UNSIGNED NOT NULL,
PRIMARY KEY(book_id),
FOREIGN KEY(author_id) REFERENCES author(author_id)
);
以下是相关的类:
@Entity
@Table(name="Author")
public class Author {
@Id
@GeneratedValue
@Column(name="AUTHOR_ID")
private long id;
@Column(name="NAME")
private String name;
@Column(name="EMAIL")
private String email;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Book> books;
//Getters and setters below...
和
@Entity
@Table(name="Book")
public class Book {
@Id
@GeneratedValue
@Column(name="BOOK_ID")
private long id;
@Column(name="TITLE")
private String title;
@Column(name="DESCRIPTION")
private String description;
@Column(name="PUBLISH_DATE")
private Date publishDate;
@ManyToOne
@JoinColumn(name="AUTHOR_ID")
private Author author;
//Getters and setters below...
每当我尝试创建SessionFactory
并保存/更新/删除数据时,我似乎必须有一个名为Author_Book
的表。如果我让Hibernate创建表模式,它会自动创建它们。但是,如果我关闭了创建,它会说它找不到表Author_Book
但我不想要那个表,因为它似乎没必要。此外,如果它创建Author_Book
我无法删除作者或书中的任何内容,因为它表示表Author_Book
中存在外键关联。
如何阻止Hibernate生成这些table1_table2模式。如果无法阻止它,或者由于某些原因该表很重要,如何在没有Hibernate的情况下删除/更新信息,说明Author_Book
表中存在外键关系?
编辑:当我尝试transaction.commit()
时出现错误。我得到以下堆栈跟踪:
Exception in thread "main" 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:453)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345)
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:1218)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:421)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at main.Test.main(Test.java:46)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'author1234.author_book' doesn't exist
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:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2643)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)