簡體   English   中英

Hibernate session.get()顯示為null

[英]Hibernate session.get() shows null

雖然我在stackoverflow上讀取了session.get null問題並嘗試按照他們的建議行事,但我的問題似乎還在繼續。

SessionFactory mysessionfactory = new Configuration().configure().buildSessionFactory();
Session mysession = mysessionfactory.openSession();
mysession.beginTransaction();

Myperson person3 = (Myperson) mysession.get(Myperson.class, 3);
System.out.println("there you go : "+person3.getName());

看起來很簡單,我只是想從數據庫中檢索數據。 我來告訴你數據庫。

如您所見,我想要檢索的數據有一個名字!

這就是我得到的,沒有! 最后,我運行調試模式的代碼,我希望你能理解這個問題,並建議一個解決方案。

在此輸入圖像描述

我在這里發生沖突,我的數據庫顯示UserId為3的數據有一個名字,但為什么我不能在輸出中看到它?

編輯:這是我要求的hibernate xml文件。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 <hibernate-configuration>
    <session-factory>

      <property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
      </property>

      <property name="hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
      </property>

      <property name="connection.pool.size">1</property>

      <property name="hibernate.connection.url">
        jdbc:mysql://localhost:3306/myhibernatedb
      </property>

      <property name="hibernate.connection.username">
       root
      </property>

      <property name="hibernate.connection.password">
       000003
      </property>

      <property    name="hibernate.current_session_context_class">thread</property>

      <property name="show_sql">true</property>

      <property name="hbm2ddl.auto">update</property>

      <!-- List of XML mapping files -->

      <mapping class="org.emreverim.com.Myperson"/>
      <mapping class="org.emreverim.com.Vehicle" />
      <mapping class="org.emreverim.com.FourWheeler" />
      <mapping class="org.emreverim.com.TwoWheeler" />

    </session-factory>
  </hibernate-configuration>

編輯2:這里是你請求的myperson類。

@Entity
public class Myperson {

    @Id @GeneratedValue(strategy=GenerationType.AUTO)   
    private int userID;     
    private String name;    


    @OneToMany(cascade=CascadeType.PERSIST)
    private Collection<Vehicle> vehicle = new ArrayList<Vehicle>();


    public Collection<Vehicle> getVehicle() {
        return vehicle;
    }
    public void setVehicle(Collection<Vehicle> vehicle) {
        this.vehicle = vehicle;
    }

    @Lob
    private String description;
    @Temporal (TemporalType.DATE)
    private Date joinedDate;


    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Date getJoinedDate() {
        return joinedDate;
    }
    public void setJoinedDate(Date joinedDate) {
        this.joinedDate = joinedDate;
    }
    public int getUserID() {
        return userID;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public void setUserID(int userID){

        this.userID = userID;
    }

}

在實體映射中,您忘記將@column屬性注釋添加到字段中,並將@table鏈接到表中。

例:

@Column(name="description")
private String description;

請查看此示例以獲取注釋http://archive.oreilly.com/pub/a/onjava/2007/02/08/an-introduction-to-hibernate-3-annotations.html?page=2

我認為這里的問題是由Hibernate Session.get()方法需要一個Seriliazable id對象引起的,這個對象在你的例子中沒有提供mysession.get(Myperson.class, 3)

因為這里的給定值3是原始類型int ,它只包含值而且沒有序列化,所以你必須使用新的Integer()方法來傳遞一個Integer序列化對象或新的Long()來傳遞一個Long序列化對象到get()方法,所以只需更改:

Myperson person3 = (Myperson) mysession.get(Myperson.class, 3);

至:

Myperson person3 = (Myperson) mysession.get(Myperson.class, new Integer(3));

要么:

Myperson person3 = (Myperson) mysession.get(Myperson.class, new Long(3));

我們碰巧看到了解決方案,我的數據庫已配置為utf-16,而它應該是utf-8。 所以它就像那樣簡單。 感謝你所有的擔憂,我為所有的答案付出了沉重的代價。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM