单表继承WITHOUT Discriminator列 [英] Single Table Inheritance WITHOUT Discriminator column
问题描述
这开始令人讨厌 - 一件简单的事情,但几个小时的挣扎,我是否变老了?
p>我试图使用Hibernate使用JPA将两个类映射到单个表。这个想法是在父类中只有一小部分的列,而在子类中只有很大一部分的子集。涉及NO TABLE继承,只有类继承。
这怎么实现?
这样做不起作用:
@Entity
@Table(name =the_table)
类父实现Serializable {
}
@Entity
@ Table(name =the_table)
class Child extends Parent实现Serializable {
}
Hibernate假设默认继承策略为InheritanceType.SINGLE_TABLE,并且正在查找鉴别器列--DYPE默认情况下。但是请稍等 - 没有表继承,鉴别器列不会使用户识别。
我也查看了PolymorphismType.EXPLICIT,这没有任何区别。堆栈跟踪是:
原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列'apprentice0_.DTYPE 在 '处sun.reflect sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
,其中在sun.reflect.NativeConstructorAccessorImpl.newInstance0条款'
(本机方法)
。 DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
在java.lang.reflect.Constructor.newInstance(Constructor.java:513)
在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:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand (MysqlIO.java:1990)
在com.mysql.jdbc.MysqlIO.sqlQueryDirect (MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
。在com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2281)
在com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
。在org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)在org.hibernate.loader.Loader.getResultSet(Loader.java:1808)处
在org.hibernate.loader.Loader处为
。 doQuery(Loader.java:697)
在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
在org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
是的,还有一件事:
@MappedSuperclass
和 @Embeddable
是没有用的,因为它们不能与 @Entity
- 父类h作为一个 @Entity
本身,因为它被用于其他地方的持久化。
@MappedSuperclass
是必须使用的注释。如果同一个类既是一个映射的超类又是一个实体,那么简单地将它分成两个类:
@MappedSuperclass
public class Parent {
// ...
}
@Entity
public class ParentEntity extends Parent {
//完全没有代码这里
}
@Entity
公共类Child继承父{
//其他字段和方法
}
Good Morning, my Dear comrades,
This is starting to be come annoying - a simple thing, but hours of struggle, am I getting old??
I am trying to map two Classes to a single table using JPA by Hibernate. The idea is to have only a small subset of columns in parent Class, and bigger/full set in the child Class. There is NO TABLE inheritance involved, only class inheritance. How can this be accomplished??
Doing this will not work:
@Entity
@Table(name = "the_table")
class Parent implements Serializable {
}
@Entity
@Table(name = "the_table")
class Child extends Parent implements Serializable {
}
Hibernate assumes default inheritance strategy InheritanceType.SINGLE_TABLE, and is looking for discriminator column - DTYPE by default. But wait - there is no table inheritance, having the discriminator column does not make sence.
I have also taken a look at PolymorphismType.EXPLICIT which did not make any difference. The stack trace is:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'apprentice0_.DTYPE' in 'where clause'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
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:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2281)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)
at org.hibernate.loader.Loader.doQuery(Loader.java:697)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
yeah, one more thing:
The @MappedSuperclass
and @Embeddable
are of no use as these can not be used in conjunction with @Entity
- the parent class has to be an @Entity
itself as it is being used for persistence elsewhere.
@MappedSuperclass
is the annotation that must be used. If the same class is both a mapped super class and an entity, then simply split it into two classes:
@MappedSuperclass
public class Parent {
// ...
}
@Entity
public class ParentEntity extends Parent {
// no code at all here
}
@Entity
public class Child extends Parent {
// additional fields and methods here
}
这篇关于单表继承WITHOUT Discriminator列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!