问题描述
假设我有Foo这样的实体 -
Let's say I have entity Foo like -
package com.some.company.model;
// imports
@Entity
public class Foo{
@Id
private Long id;
// getters / setters and other properties omitted
}
所以当通过HQL处理实体时,我更喜欢通过完全限定的类名来引用实体 - 比如 -
so while dealing with Entity through HQL I prefer to refer the Entity by fully qualified class name like -
entityManager.createQuery(String.format("delete from %s where id = :id", Foo.class.getName()))
.setParameter("id", fooId)
.executeUpdate();
我注意到 @Entity
注释中的一件事 - 默认情况下,name属性具有实体类的非限定名称。这让我想到为什么不合格的名字?
我应该在HQL中使用非限定名称还是完全限定名称?
I noticed one thing in @Entity
annotation - the name property has a unqualified name of the entity class by default. which makes me think why unqualified name?
Which should I use in HQL unqualified name or fully qualified name?
推荐答案
使用完全限定名称,因为Hibernate不允许重复的实体名称。所以,如果你有不同的包装中具有相同名称的实体。 Hibernate会抛出一个DuplicateMappingException异常。
There is no point to use the fully qualified name because Hibernate won't allow duplicate entity names. So if you had different entities with the same name, in different packages. Hibernate will throw a DuplicateMappingException.
这篇关于HQL与完全合格的类名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!