Java Hibernate Annotations:实体没有默认构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25452018/
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
Hibernate Annotations : No default constructor for entity
提问by user3968762
I am trying to persist the objects generated by JAXB. Here is the sample structure:
我正在尝试保留 JAXB 生成的对象。这是示例结构:
@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. That String I want to persist in the database as a part of the whole record. I am seeing
现在,RecipientDetails
该类有一个参数构造函数,它接受一个字符串。我想将该字符串作为整个记录的一部分保留在数据库中。我在看
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:
当我尝试保存对象时出现异常。我有两个问题:
Do we have any work around this exception? I can't change the class as it is designed for JAXB marshalling/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. I want it to ignore the rest of the records if it has more than 1 record. Is it possible?
Is this good design to use the annotation directly into classes which are generated by JAXB? Should I create another classes (and possibly mappers/converters) just to store and retrieve the information?
我们是否有任何解决此异常的方法?我无法更改该类,因为它是为 JAXB 编组/解组而设计的。我可以在不改变结构的情况下以某种方式存储对象吗?此外,我只对存储收件人详细信息引用的列表的第一条记录感兴趣,因为我只需要一行对象。如果它有超过 1 条记录,我希望它忽略其余的记录。是否可以?
直接在 JAXB 生成的类中使用注解是一种很好的设计吗?我是否应该创建另一个类(可能还有映射器/转换器)来存储和检索信息?
采纳答案by chiastic-security
For your first question: this is happening because when Hibernate tries to create a bean, it does it via reflection. It does the object creation by calling the no-arg constructor, and then using the setter methods to set the properties. You can't use a bean that doesn't have a no-arg constructor.
对于您的第一个问题:发生这种情况是因为当 Hibernate 尝试创建 bean 时,它是通过反射来实现的。它通过调用无参数构造函数来创建对象,然后使用 setter 方法设置属性。您不能使用没有无参数构造函数的 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. 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 (i.e., doesn't have private methods that you then can't get to).
对于第二个问题:如果其他东西为你生成了没有无参数构造函数的类,那么你唯一的选择(如果你不能修改类)是围绕它创建一个包装器,或者一个子类有一个无参数构造函数。如果您不能直接修改类,我看不到任何其他方法。但是子类化应该没问题,只要您拥有的类对方法有足够的可见性(即,没有您无法访问的私有方法)。