我想知道使用@SqlResultSetMapping和@ConstructorResult的好处是什么?是否更好,如果,为什么,使用JPQL SELECT NEW SomeConstructor?
例如,更好:
@Table(name = "person")
@Entity
@SqlResultSetMappings(
@SqlResultSetMapping(name = "contactLess",
classes = @ConstructorResult(columns = {
@ColumnResult(name = "id", type = Long.class),
@ColumnResult(name = "full_name", type=String.class)
}, targetClass = Person.class)
)
)
@NamedNativeQueries({
@NamedNativeQuery(name = "Person.findWithoutContacts",
query = "SELECT c.id, c.full_name FROM person c ",
resultSetMapping = "contactLess", resultClass = Person.class)
})
比这个(本例中的弹簧数据):
@Query("SELECT NEW Person(c.id, c.fullName) FROM Person c")
public List<Person> findAll();
答案 0 :(得分:1)
如果您有几个JPQL(和/或查询构建器)引用一个实体,那么使用注释将使您的代码更容易重构。 JPQL也没有编译安全性。