Java hibernate加入继承(未找到)

时间:2012-10-29 23:28:18

标签: java hibernate inheritance postgresql-9.1 hibernate-annotations

对不起我的英语,它不是我的母语,而且它已经是2点钟了,所以现在不能想到海峡。 现在我在使用Java和PostgreSQL的hibernate时遇到了下一个问题: 我有实体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)
...

(我剪掉了日志的尾部,而它响应了invokin 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部分​​中没有指定和获取它。

我的尝试:

  1. 在类User中创建@Column私有Integer id(获取Annotation 覆盖类的主要列的解析器异常;;
  2. 在类User中创建@Column private Integer并将其设置为     PrimaryKeyJoinColumn(但我得到了(应该映射到     插入="假" update =" false")即使我将其映射为     @JoinColumn(插入=假,可更新=假,referencedColumnName =" ID&#34));
  3. 创建专栏" hid"并将其指向为PrimaryKeyJoinColumn     列本身的注释,而不是整个类(得到相同的错误     in original,column" human0_1_.id not found);
  4. 任何人,请给我一些想法!

2 个答案:

答案 0 :(得分:2)

您的课程应如下所示:
人类

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Human implements Serializable {

    private static final long serialVersionUID = 1L;

    public static enum Gender {
        Male, Female
    }

    @Id
    @GeneratedValue
    private Long id;

    @Enumerated(EnumType.STRING)
    private Gender gender;
    private String firstName;
    private String lastName;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
   .....
}

用户

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@PrimaryKeyJoinColumn
public class User extends Human {

    private static final long serialVersionUID = 1L;

    @Column
    @Temporal(TemporalType.DATE)
    private Date registrationDate;

    public Date getRegistrationDate() {
        return registrationDate;
    }

    public void setRegistrationDate(Date registrationDate) {
        this.registrationDate = registrationDate;
    }
    ....
}
  • 人类应该通过@Entity注释
  • 进行注释
  • 模型实体类应实现Serializable接口

  1. Inheritance Type (With Hibernate)
  2. Inheritance Type JOINED

答案 1 :(得分:2)

问题解决了,主要归功于myhaylo 这个问题的原因始终由SQE PMD插件标记 - “User”是SQL-99语法中的关键字。由于这个(看起来像),需要引用表名。它在PgAdmin查询浏览器中引用,但不是在Hibernate生成的查询中,实际上,在那种情况下,必须指向user.id的human0_1_.id指向无处。
将类和表名更改为其他名称(如AndhorUser)后,现在发布的原始数据库结构存在问题,类定义运行良好。