postgresql 如何在 Hibernate 中处理没有 PRIMARY KEY 的表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11983493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:12:13  来源:igfitidea点击:

How to work with tables without PRIMARY KEY in Hibernate?

javahibernateclasspostgresql

提问by Kliver Max

i have table

我有桌子

CREATE TABLE test_wopk
(
  id integer,
  "name" character(25),
  age integer
)

After Reverse Ingeniring in hibernate i get a classes and mapping file.

在 hibernate 中反向 Ingeniring 后,我得到一个类和映射文件。

TestWopk.java

测试Wopk.java

package gen;

// Generated 16.08.2012 14:08:26 by Hibernate Tools 3.4.0.CR1

/**
 * TestWopk generated by hbm2java
 */
public class TestWopk implements java.io.Serializable
{

private TestWopkId id;

public TestWopk()
{
}

public TestWopk(TestWopkId id)
{
    this.id = id;
}

public TestWopkId getId()
{
    return this.id;
}

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

}

TestWopkId.java

TestWopkId.java

package gen;

// Generated 16.08.2012 14:08:26 by Hibernate Tools 3.4.0.CR1

/**
 * TestWopkId generated by hbm2java
 */
public class TestWopkId implements java.io.Serializable
{

private Integer id;
private String name;
private Integer age;

public TestWopkId()
{
}

public TestWopkId(Integer id, String name, Integer age)
{
    this.id = id;
    this.name = name;
    this.age = age;
}

public Integer getId()
{
    return this.id;
}

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

public String getName()
{
    return this.name;
}

public void setName(String name)
{
    this.name = name;
}

public Integer getAge()
{
    return this.age;
}

public void setAge(Integer age)
{
    this.age = age;
}

public boolean equals(Object other)
{
    if ((this == other))
        return true;
    if ((other == null))
        return false;
    if (!(other instanceof TestWopkId))
        return false;
    TestWopkId castOther = (TestWopkId) other;

    return ((this.getId() == castOther.getId()) || (this.getId() != null && castOther.getId() != null && this.getId()
            .equals(castOther.getId())))
            && ((this.getName() == castOther.getName()) || (this.getName() != null && castOther.getName() != null && this
                    .getName().equals(castOther.getName())))
            && ((this.getAge() == castOther.getAge()) || (this.getAge() != null && castOther.getAge() != null && this
                    .getAge().equals(castOther.getAge())));
}

public int hashCode()
{
    int result = 17;

    result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());
    result = 37 * result + (getName() == null ? 0 : this.getName().hashCode());
    result = 37 * result + (getAge() == null ? 0 : this.getAge().hashCode());
    return result;
}

}

TestWopk.hbm.xml

测试Wopk.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 16.08.2012 14:08:26 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="gen.TestWopk" table="test_wopk">
    <composite-id name="id" class="gen.TestWopkId">
        <key-property name="id" type="java.lang.Integer">
            <column name="id" />
        </key-property>
        <key-property name="name" type="string">
            <column name="name" length="25" />
        </key-property>
        <key-property name="age" type="java.lang.Integer">
            <column name="age" />
        </key-property>
    </composite-id>
</class>
</hibernate-mapping>

Iwant to get data from db.

我想从数据库中获取数据。

    Session session = HibernateUtil.currentSession();

    Transaction tx= session.beginTransaction();
    //чтение из бд
     List list = session.createQuery("from TestWopk").list();
 Iterator itr = list.iterator();
 while(itr.hasNext()){
   TestWopk test = (TestWopk) itr.next();
   System.out.print("EmpName: "+ test.getId());
   System.out.print(" EmpSal: "+ test.getName());
   System.out.print(" EmpSal: "+ test.getAge());
   System.out.println();
 }
    tx.commit();

    HibernateUtil.closeSession();

And get Error

并得到错误

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method getName() is undefined for the type TestWopk
The method getAge() is undefined for the type TestWopk

at net.sf.hibernate.examples.quickstart.hib_main.main(hib_main.java:67)

Tell me please how to work with classes which was generated by hibernate?

请告诉我如何使用由休眠生成的类?

回答by gresdiplitude

Like the compilation error is telling you, your TestWopk.java class does not have a getName() & getAge() methods. Change your while loop to

就像编译错误告诉您的那样,您的 TestWopk.java 类没有 getName() 和 getAge() 方法。将您的 while 循环更改为

        while(itr.hasNext()){    
        TestWopk test = (TestWopk) itr.next();
        System.out.print("EmpName: " + test.getId().getId());
        System.out.print(" EmpSal: " + test.getId().getName());
        System.out.print(" EmpSal: " + test.getId().getAge());
        System.out.println();
    }