当我尝试运行我的代码时,它会生成以下异常。
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select sequence_next_hi_value from hibernate_sequences where sequence_name = 'Ancestor' for update
Hibernate: insert into hibernate_sequences(sequence_name, sequence_next_hi_value) values('Ancestor', ?)
Hibernate: update hibernate_sequences set sequence_next_hi_value = ? where sequence_next_hi_value = ? and sequence_name = 'Ancestor'
Hibernate: insert into Ancestor (nationality, DTYPE, id) values (?, 'Ancestor', ?)
Dec 05, 2012 1:14:43 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1054, SQLState: 42S22
Dec 05, 2012 1:14:43 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unknown column 'DTYPE' in 'field list'
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Unknown column 'DTYPE' in 'field list'
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDeleg ate.java:122)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy10.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2859)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3300)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at com.mappings.util.HibernateUtil.commitTransaction(HibernateUtil.java:70)
at com.mappings.main.MappingsTest.main(MappingsTest.java:33)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'DTYPE' in 'field list'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1541)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1455)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1440)
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)
... 17 more
当我尝试在表中插入数据时,代码生成异常。我无法理解为什么会产生此异常。以下是我的代码: -
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Ancestor {
private int id;
private String nationality;
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
}
@Entity
public class Parent extends Ancestor{
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
@Entity
public class Child extends Parent{
private String lastName;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
public class MappingsTest {
public static void main(String[] args) {
HibernateUtil.recreateDatabase();
Ancestor ancestor = new Ancestor();
ancestor.setNationality("Indian");
Ancestor ancestor1 = new Ancestor();
ancestor1.setNationality("Indian");
Parent parent = new Parent();
parent.setNationality("India");
parent.setFirstName("Harmeet Singh");
Child child = new Child();
child.setNationality("Indian");
child.setFirstName("Harmeet Singh");
child.setLastName("Kalra");
Session session = HibernateUtil.beginTransaction();
session.saveOrUpdate(ancestor);
session.saveOrUpdate(parent);
session.saveOrUpdate(child);
session.saveOrUpdate(ancestor1);
HibernateUtil.commitTransaction();
}
}
答案 0 :(得分:1)
您的架构缺少鉴别器列(DTYPE
是其默认名称),用于区分多态实体的类型(Ancestor
和Parent
)。
如果手动创建架构,则需要添加该列(可以使用@DiscriminatorColumn
配置其名称),或者让Hibernate生成新架构。