简体   繁体   English

Hibernate Annotations:没有实体的默认构造函数

[英]Hibernate Annotations : No default constructor for entity

I am trying to persist the objects generated by JAXB. 我试图坚持JAXB生成的对象。 Here is the sample structure: 以下是示例结构:

@Column(name = "reporting_identifier")
private String reportingIdentifier;
@Column(name = "apply_quiet_time")
private boolean applyQuietTime;
@Embedded
private RecipientDetailsList recipientDetailsList;

Below is the structure of RecipientDetailsList class: 下面是RecipientDetailsList类的结构:

@ElementCollection(targetClass=String.class)
private List<RecipientDetails> recipientDetails;

Now, the RecipientDetails class has one argument constructor, which accepts a String. 现在, RecipientDetails类有一个参数构造函数,它接受一个String。 That String I want to persist in the database as a part of the whole record. 我希望将字符串作为整个记录的一部分保存在数据库中。 I am seeing 我看到了

org.hibernate.InstantiationException: No default constructor for entity: RecipientDetailsList org.hibernate.InstantiationException:没有实体的默认构造函数:RecipientDetailsList

exception when I try to save an object. 我尝试保存对象时出现异常。 I have two questions: 我有两个问题:

  1. Do we have any work around this exception? 我们有关于此例外的任何工作吗? I can't change the class as it is designed for JAXB marshalling/unmarhsalling. 我无法更改类,因为它是为JAXB编组/ unmarhsalling而设计的。 Can I somehow store the objects without altering the structure? 我可以以某种方式存储对象而不改变结构吗? Also, I am interested in only storing the first record of the list referenced by recipientDetails as I want only one row for object. 此外,我感兴趣的是只存储recipientDetails引用的列表的第一条记录,因为我只想要一行对象。 I want it to ignore the rest of the records if it has more than 1 record. 如果它有超过1条记录,我希望它忽略其余的记录。 Is it possible? 可能吗?

  2. Is this good design to use the annotation directly into classes which are generated by JAXB? 这个好的设计是否可以直接将注释用于由JAXB生成的类中? Should I create another classes (and possibly mappers/converters) just to store and retrieve the information? 我应该创建另一个类(可能还有mappers /转换器)来存储和检索信息吗?

For your first question: this is happening because when Hibernate tries to create a bean, it does it via reflection. 对于你的第一个问题:这种情况正在发生,因为当Hibernate尝试创建一个bean时,它会通过反射来实现。 It does the object creation by calling the no-arg constructor, and then using the setter methods to set the properties. 它通过调用no-arg构造函数创建对象,然后使用setter方法设置属性。 You can't use a bean that doesn't have a no-arg constructor. 您不能使用没有no-arg构造函数的bean。

For the second question: if something else has generated classes for you that don't have a no-arg constructor, really your only option (if you can't modify the class) is to create a wrapper round it, or a subclass that has a no-arg constructor. 对于第二个问题:如果其他东西为你生成了没有no-arg构造函数的类,那么你唯一的选择(如果你不能修改类)就是创建一个包装它的包装器,或者是一个子类有一个无参数的构造函数。 I don't see any other way of doing it if you can't modify the class directly. 如果你不能直接修改类,我没有看到任何其他方法。 But the subclassing should be fine as long as the class you've got has enough visibility on the methods (ie, doesn't have private methods that you then can't get to). 但是,只要你获得的类对方法有足够的可见性(即,没有你无法获得的私有方法),子类化应该没问题。

If you mark RecipientDetails as @Embeddable hibernate will use the constructor with arguments. 如果将RecipientDetails标记为@Embeddable hibernate将使用带参数的构造函数。

@Embeddable
class RecipientDetails {

    //no default constructor

    public RecipientDetails(some arg) {}


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM