在 spring 中使用 hibernate 时,通常我们将代码编写为如下所示的接口
public interface UserRepository {
Collection<User> findByLastName(String lastName) throws DataAccessException;
//more methods
}
@Repository
public class JpaUserRepositoryImpl implements UserRepository {
@PersistenceContext
private EntityManager em;
@SuppressWarnings("unchecked")
public Collection<User> findByLastName(String lastName) {
Query query = this.em.createQuery("SELECT DISTINCT user FROM User user WHERE user.lastName LIKE :lastName");
query.setParameter("lastName", lastName + "%");
return query.getResultList();
}
}
@Entity
@Table(name = "users")
public class User {
..
}
我们如何在 Panache 中做同样的事情?
public interface UserRepository extends PanacheRepositoryBase<User, Integer> {
Collection<User> findByLastName(String lastName) ;
//more methods
}
@ApplicationScoped
public class JpaUserRepositoryImpl implements UserRepository {
@Inject
private EntityManager em;
@SuppressWarnings("unchecked")
public Collection<User> findByLastName(String lastName) {
Query query = this.em.createQuery("SELECT DISTINCT user FROM User user WHERE user.lastName LIKE :lastName");
query.setParameter("lastName", lastName + "%");
return query.getResultList();
}
}
它抛出
[2] Unsatisfied dependency for type javax.persistence.EntityManager and qualifiers [@Default]
- java member: org....repository.jpa.JpaUserRepositoryImpl#em
- declared on CLASS bean [types=[org....repository.jpa.JpaUserRepositoryImpl, org....repository.UserRepository, io.quarkus.hibernate.orm.panache.PanacheRepositoryBase<org.....model.User, java.lang.Integer>, java.lang.Object], qualifiers=[@Default, @Any], target=org.....repository.jpa.JpaUserRepositoryImpl]
[