hibernate对象列表

时间:2013-11-15 08:25:16

标签: hibernate

当我尝试获取一个对象列表时,我得到以下异常:

  Hibernate: select this_.newsId as newsId2_0_, this_.newsBrief as newsBrief2_0_, this_.newsContent as newsCont3_2_0_, this_.newsDate as newsDate2_0_, this_.newsTitle as newsTitle2_0_, this_.selected as selected2_0_ from NEWS this_
    Nov 15, 2013 11:04:58 AM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [action] in context with path [/strts-spring-hbnt-nomaven] threw exception [org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
    java.sql.SQLSyntaxErrorException: ORA-00904: "THIS_"."SELECTED": invalid identifier

DDL

--------------------------------------------------------
--  DDL for Table NEWS
--------------------------------------------------------

  CREATE TABLE "SYSTEM"."NEWS" 
   (    "NEWSID" NUMBER, 
    "NEWSTITLE" VARCHAR2(100 BYTE), 
    "NEWSBRIEF" VARCHAR2(500 BYTE), 
    "NEWSCONTENT" VARCHAR2(2048 BYTE), 
    "NEWSDATE" DATE
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "SYSTEM" ;

--------------------------------------------------------
--  DDL for Index NEWS_PK
--------------------------------------------------------

  CREATE UNIQUE INDEX "SYSTEM"."NEWS_PK" ON "SYSTEM"."NEWS" ("NEWSID") 
  PCTFREE 10 INITRANS 2 MAXTRANS 255 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "SYSTEM" ;
--------------------------------------------------------
--  Constraints for Table NEWS
--------------------------------------------------------

  ALTER TABLE "SYSTEM"."NEWS" ADD CONSTRAINT "NEWS_PK" PRIMARY KEY ("NEWSID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "SYSTEM"  ENABLE;
  ALTER TABLE "SYSTEM"."NEWS" MODIFY ("NEWSDATE" NOT NULL ENABLE);
  ALTER TABLE "SYSTEM"."NEWS" MODIFY ("NEWSCONTENT" NOT NULL ENABLE);
  ALTER TABLE "SYSTEM"."NEWS" MODIFY ("NEWSBRIEF" NOT NULL ENABLE);
  ALTER TABLE "SYSTEM"."NEWS" MODIFY ("NEWSTITLE" NOT NULL ENABLE);
  ALTER TABLE "SYSTEM"."NEWS" MODIFY ("NEWSID" NOT NULL ENABLE);

新闻类:

@Entity
@Table(name="NEWS")
public class News {

    private boolean selected=false;
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "news_seq_gen")
    @SequenceGenerator(name = "news_seq_gen", sequenceName = "news_seq")
    private int newsId;

    @Column(name="newsTitle", nullable = false, length = 100)
    private String newsTitle;
    @Column(name="newsDate", nullable = false)
    private Date newsDate;
    @Column(name="newsBrief", nullable = false, length = 500)
    private String newsBrief;
    @Column(name="newsContent", nullable = false, length = 2048)
    private String newsContent;
        //getters and setters 
}

查询:

return sessionFactory.getCurrentSession().createQuery("from News as News").list();

为什么hibernate会在列名中添加数字?问题出在哪里? 任何建议都会受到赞赏。

1 个答案:

答案 0 :(得分:3)

您通过将@Id放在类属性上来使用字段访问模式。这会导致hibernate将所有字段视为表列。据我了解你的DDL,News.selected是一个瞬态属性。

尝试将@Transient注释放在selected上。