java - Hibernate 返回 null 的列表

标签 java hibernate

我正在尝试使用 Hibernate,但是当我执行查询时,我收到一个实体列表,其中所有属性均为 NULL。问题是,当我直接使用 jdbc 时,我可以从我的数据库中检索值。 我读过有关 NULL 值的类似文章,但无法弄清楚我的情况出了什么问题。 下面你可以看到: 1)我的实体:Eshop

public class Eshop  implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Eshop() {}

    public Eshop(int eshopId, String code, String name, String lastModified) {
    this.eshopId = eshopId;
    this.code = code;
    this.name = name;
    this.lastModified = lastModified;
}

    public int eshopId;

    public String code;

    public String name;

    public String lastModified;

    public int getEshopId() {
        return eshopId;
    }

    public void setEshopId(int eshopId) {
        eshopId = eshopId;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        code = code;
    }


    public String getName() {
        return name;
    }

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


    public String getLastModified() {
        return lastModified;
    }

    public void setLastModified(String lastModified) {
        lastModified = lastModified;
    }

2) Hibernate 映射

<hibernate-mapping>
   <class name="dataModel.Eshop" table="Eshop">
      <meta attribute="class-description">
         This class contains the Eshop details. 
      </meta>
      <id name="eshopId" type="int" column="EshopId">
         <generator class="native"/>
      </id>
      <property name="code" column="Code" type="string"/>
      <property name="name" column="Name" type="string"/>
      <property name="lastModified" column="LastModified" type="string"/>
   </class>
</hibernate-mapping>

这就是我运行查询的方式:

Configuration config = new Configuration().configure(); 
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();

Query hibernateQuery = session.createQuery("from Eshop");
List<Eshop> Eshops = hibernateQuery.list();

因此,当我运行查询时,我收到了数据库中 Eshop 的确切数量,但它们的所有属性值均为空! 一个想法??谢谢您

最佳答案

你所有的设置者都是错误的:

public void setEshopId(int eshopId) {
    eshopId = eshopId;
}

这是一个空话。应该是

public void setEshopId(int eshopId) {
    this.eshopId = eshopId;
}

关于java - Hibernate 返回 null 的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35729578/

相关文章:

java - 如何使用正则表达式替换 java 中的标记?

java - JPA 中的日期时间表达式作为参数传递

Java 甲骨文异常 - "maximum number of expressions in a list is 1000"

Java/Hibernate - 只读模式下不允许写操作

java - 如何使用 hibernate 正确映射嵌套子类

java - JLayeredPane 与 JPanel

java - 如何打开单声道

java - 将 2 个字符串数组的值合并到一个串联字符串数组中

java - 什么时候抛出 FileNotFoundException

java - org.hibernate.ejb.QueryImpl 无法转换为 spring roo 中的类