java 休眠:无法在类上找到合适的构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12363867/
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-31 08:34:56  来源:igfitidea点击:

hibernate: Unable to locate appropriate constructor on class

javahibernate

提问by Frazer

Here is my class:

这是我的课:

public class TrainLate {

private int id;
private Date startDate;
private Date endDate;
private Set<TrainSchedule> ts=new HashSet<TrainSchedule>(); 
public TrainLate(){}
public TrainLate(int id, Date startDate, Date endDate) {
    super();
    this.id = id;
    this.startDate = startDate;
    this.endDate = endDate;
}

    // setters and getters...
}

The type Date is java.sql.Date

日期类型是 java.sql.Date

In another class I use HQL:

在另一堂课中,我使用 HQL:

String hql="SELECT new TrainLate(id,startDate,endDate) FROM TrainLate "+                "WHERE id="+String.valueOf(index);

where index is an int parameter.

其中 index 是一个 int 参数。

Here is the "TrainLate.hbm.xml":

这是“TrainLate.hbm.xml”:

<class name="classes.TrainLate">
    <id name="id">
        <generator class="native"/>
    </id>
    <property name="startDate"/>
    <property name="endDate"/>
    <set name="ts"  lazy="false" cascade="all-delete-orphan" inverse="true">
           <key column="trainLateID" />
           <one-to-many class="classes.TrainSchedule" />
          </set>  
</class>

Here is the exception:

这是例外:

Unable to locate appropriate constructor on class [classes.TrainLate] [SELECT new TrainLate(id,startDate,endDate) FROM classes.TrainLate WHERE id=0]

Where "classes" is the package name.

其中“classes”是包名。

回答by dcernahoschi

First: Hibernate requires your entities to have a default no argument constructor.

第一:Hibernate 要求您的实体具有默认的无参数构造函数。

Second: Your hql should be: "from TrainLate t where t.id = :id".

第二:你的 hql 应该是:"from TrainLate t where t.id = :id"

String hql = "from TrainLate t where t.id = :id";
List<TrainLate> result = (List<TrainLate>) session.createQuery(hql).setParameter("id", 1).list();

Or even better. When you know the id of the entity you don't need to search with hql:

或者甚至更好。当您知道实体的 id 时,您无需使用 hql 进行搜索:

TrainLate t = session.get(TrainLate.class, 1L); // I assume your id is a Long

This one returns nullif the entity is not found.

null如果未找到实体,则返回此值。

or

或者

TrainLate t = session.load(TrainLate.class, 1L); // I assume your id is a Long

This one throws an ObjectNotFoundExceptionif the entity is not found.

ObjectNotFoundException如果未找到实体,则抛出一个。

回答by Mark Bramnik

I know it can sound silly, but have you tried to debug this thing?

我知道这听起来很傻,但你有没有试过调试这个东西?

My first thought would be (if I were hibernate :) ) to create an object of TrainLate with No-OP constructor and then invoke a series of setters to set id, startDate and endDate.

我的第一个想法是(如果我处于休眠状态:))使用 No-OP 构造函数创建一个 TrainLate 对象,然后调用一系列 setter 来设置 id、startDate 和 endDate。

I didn't see it in your code snippet...

我在你的代码片段中没有看到它......