对不起我的英语,这不是我的母语而且已经 2 点了,所以现在不能思考。现在我在使用 Java 和 PostgreSQL 的休眠时遇到了下一个问题:我有实体 Human 和 User 如下(带有注释映射):
@Inheritance(strategy = InheritanceType.JOINED)
public class Human implements Serializable{
private static final long serialVersionUID = 1L;
public static enum Gender{
Male,
Female}
@Id
@GeneratedValue
private Integer id;
@Enumerated(EnumType.STRING)
private Gender gender;
@Column
@Temporal(TemporalType.DATE)
private Date dateOfBirth;
private String firstName;
private String middleName;
private String lastName;
// getters,setters
}
@Entity
@PrimaryKeyJoinColumn
public class User extends Human{
private static final long serialVersionUID= 1L;
@Column
@Temporal(TemporalType.DATE)
private Date registrationDate;
private Boolean activationStatus;
private String activationCode;
private String login;
private String password;
private String sessionCode;
private String email;
private String country;
private String city;
private String avatarURL;
private String description;
// getters,setters
}
和数据库结构如下:
human
(
dateofbirth date NOT NULL,
firstname text NOT NULL,
middlename text NOT NULL,
lastname text NOT NULL,
id serial NOT NULL,
gender text,
CONSTRAINT "Human_id" PRIMARY KEY (id )
)
"user"
(
login text NOT NULL,
password text NOT NULL,
activationcode text NOT NULL,
activationstatus status NOT NULL,
avatarurl text NOT NULL,
city text NOT NULL,
country text NOT NULL,
description text NOT NULL,
registrationdate date NOT NULL,
email text NOT NULL,
id integer,
CONSTRAINT uid PRIMARY KEY (id )
)
当我尝试运行我的项目(简单单元测试)时,我得到下一个错误:
org.postgresql.util.PSQLException: ERROR: column human0_1_.id does not exist
Позиция: 778
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
at org.hibernate.loader.Loader.doQuery(Loader.java:674)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3044)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.tempos.server.modules.users.HumanTest.testGetId(HumanTest.java:31)
...
(我剪掉了日志的尾部,而它响应调用 JUnit 方法)。
生成的查询(失败):
select human0_.id as id3_0_, human0_.dateOfBirth as dateOfBi2_3_0_, human0_.firstName as firstName3_0_, human0_.gender as gender3_0_, human0_.lastName as lastName3_0_, human0_.middleName as middleName3_0_, human0_1_.activationCode as activati1_4_0_, human0_1_.activationStatus as activati2_4_0_, human0_1_.avatarURL as avatarURL4_0_, human0_1_.city as city4_0_, human0_1_.country as country4_0_, human0_1_.description as descript6_4_0_, human0_1_.email as email4_0_, human0_1_.login as login4_0_, human0_1_.password as password4_0_, human0_1_.registrationDate as registr10_4_0_, human0_1_.sessionCode as session11_4_0_, case when human0_1_.id is not null then 1 when human0_.id is not null then 0 end as clazz_0_ from Human human0_ left outer join User human0_1_ on human0_.id=human0_1_.id where human0_.id=?
如您所见,它尝试在human1_1_.id(最初是User.id)上加入列,但它没有在查询的SELECT部分中指定和获取。
我尝试了什么:
- 在 User 类中创建 @Column 私有整数 id(获得注释解析器异常以覆盖类的主列);
- 创建 @Column private Integer 隐藏在类 User 中并将其设置为 PrimaryKeyJoinColumn (但我得到(应该用 insert="false" update="false" 映射)即使我将它映射为 @JoinColumn(insertable=false,updatable=false ,referencedColumnName="id"));
- 创建列“隐藏”并将其指向列本身的注释中的 PrimaryKeyJoinColumn,而不是整个类(得到与原始列相同的错误,未找到列“human0_1_.id”);
任何人,请给我一些想法!