2012-08-16 31 views
0

我有表如何在Hibernate中使用沒有PRIMARY KEY的表?

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

反向Ingeniring後休眠我得到一個類和映射文件。

TestWopk.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

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

<?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從數據庫獲取數據。

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(); 

並得到錯誤

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) 

請告訴我怎麼用這是由Hibernate生成的類工作?

回答

2

像編譯錯誤告訴你,你的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(); 
    } 
+0

你是嚮導!它的作品謝謝你。但你能給我建議更好的添加到表PRIMARY KEY或使用你的方法嗎? – 2012-08-16 08:56:24

+0

如果您可以控制數據模型,即允許更改它,則總是建議不要使用複合主鍵。在你的情況下,如果ID列將具有唯一值,爲什麼不把它作爲主鍵?如果沒有創建一個具有唯一值並將其用作主鍵的新標識列。 – gresdiplitude 2012-08-16 09:11:49

相關問題