HQL with fully qualified class name

2019-02-25 07:30发布

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   
}

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();

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?

2条回答
Anthone
2楼-- · 2019-02-25 07:50

I don't think it makes a difference, but... I prefer unqualified name simply because it is shorter and it makes my HQL easier to comprehend. The only reason I can think of to use fully qualified name is you have 2 different Foo entities (say, from different packages), in this case, I would much rather map them into different entity names (say, AFoo, BFoo) rather using fully qualified names to differentiate them.

查看更多
倾城 Initia
3楼-- · 2019-02-25 07:58

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.

查看更多
登录 后发表回答