在hibernate中对视图进行查询(select语句)

时间:2017-10-11 04:16:17

标签: java database oracle hibernate hibernate-mapping

我想在现有的VIEW上显示select语句的结果。但我一直得到以下错误:找不到表并尝试创建表:

22058 [main] INFO org.hibernate.cfg.SettingsFactory  - Default entity-mode: pojo
24051 [main] INFO org.hibernate.tool.hbm2ddl.DatabaseMetadata  - table not found: MGSMS
24675 [main] INFO org.hibernate.tool.hbm2ddl.DatabaseMetadata  - table not found: MGSMS
24675 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate  - create table MGSMS (ID number(10,0) not null, DEST varchar2(255 char), MESSAGE varchar2(255 char), primary key (ID))
25969 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate  - Unsuccessful: create table MGSMS (ID number(10,0) not null, DEST varchar2(255 char), MESSAGE varchar2(255 char), primary key (ID))
25970 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate  - ORA-00955: name is already used by an existing object

我是hibernate的新手。任何人都可以帮忙。

我有dbInfo.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

 <hibernate-mapping>  
  <class name="com.javaHb.queryDBhb.dbInfo" table="MGSMS" >  
    <id column="ID" name="id" type="int">
      <generator class="increment" />
    </id>

    <property name="dest" type="string" column="DEST" /> 
    <property name="message" type="string" column="MESSAGE" />

  </class>  

 </hibernate-mapping> 

main java classTakersQuery.java

package com.javaHb.queryDBhb;

import java.util.List;

import org.apache.log4j.BasicConfigurator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class TakersQuery {
    public static void main(String[] args) {

        BasicConfigurator.configure();
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");

        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();

        //get few fields
        Query query = session.createQuery("select sms from MGSMS sms");
        List<Object[]> rows = query.list();

        for (Object[] row: rows) {
            System.out.println(" ------------------- ");
            System.out.println("dest: " + row[0]);
            System.out.println("message: " + row[1]);
        }
        tx.commit();
        session.close();
        factory.close();
    }
}

DBInfo.java是java类,即Persistent class

package com.javaHb.queryDBhb;

import javax.persistence.Column;
import javax.persistence.Entity;  
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;  
import javax.persistence.NamedNativeQuery;
import javax.persistence.Table;  

import org.hibernate.annotations.Immutable;
import org.hibernate.annotations.Loader;

@Entity
@Immutable
public class dbInfo {
    @Id  
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private int id;
    @Column(name="dest")
    private String dest; 
    @Column(name="message")
    private String message;

    public int getid() {  
        return id;  
    }  
    public void setid(int id) {  
        this.id = id;  
    }  
    public String getdest() {  
        return dest;  
    }  
    public void setdest(String dest) {  
        this.dest = dest;  
    }  
    public String getmessage() {  
        return message;  
    }  
    public void setmessage(String message) {  
        this.message = message;  
    }  

}

1 个答案:

答案 0 :(得分:0)

i had to correct the column mapping and it worked