Я новичек в Hibernate.
Есть UserRepository
и RestController
.
Хочу сделать поиск по одному из полей юзера: Имя, фамилия, пол.
Если с получением всех юзеров, поиск по id все понятно. Но как сделать выборку только по одному полю?
public interface UserRepository {
UserEntity save(UserEntity car);
void update(UserEntity user);
List<UserEntity> getAll();
List<UserEntity> getAllByFirstName();
List<UserEntity> getAllByLastName();
List<UserEntity> getAllByGender();
UserEntity getById(Long id);
void remove(Long id);
}
@Repository
@Transactional()
public class UserRepositoryImpl implements UserRepository {
@PersistenceContext
private EntityManager entityManager;
@Override
@Transactional
public UserEntity save(UserEntity user) {
if(user.getId() != null){
return entityManager.merge(user);
} else {
entityManager.persist(user);
return user;
}
}
@Override
public void update(UserEntity user) {
}
@Override
public List<UserEntity> getAll() {
CriteriaQuery<UserEntity> criteria = entityManager.getCriteriaBuilder().createQuery(UserEntity.class);
criteria.select(criteria.from(UserEntity.class));
// criteria.where( builder.equal( root.get( Person_.name ), "John Doe" ) );
return entityManager.createQuery(criteria).getResultList();
}
@Override
public List<UserEntity> getAllByFirstName() {
return null;
}
@Override
public List<UserEntity> getAllByLastName() {
return null;
}
@Override
public List<UserEntity> getAllByGender() {
return null;
}
@Override
public UserEntity getById(Long id) {
return entityManager.find(UserEntity.class, id);
}
@Override
public void remove(Long id) {
UserEntity user = getById(id);
if(user != null) {
entityManager.remove(user);
}
}
}
Service:
public interface UserService {
UserEntity save(UserEntity userEntityy);
List<UserEntity> getAll();
UserEntity getById(long id);
void remove(long id);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserRepository userRepository;
@Override
public UserEntity save(UserEntity userEntityy) {
return userRepository.save(userEntityy);
}
@Override
public List<UserEntity> getAll() {
return userRepository.getAll();
}
@Override
public UserEntity getById(long id) {
return userRepository.getById(id);
}
@Override
public void remove(long id) {
userRepository.remove(id);
}
}
Оборудование для ресторана: новинки профессиональной кухонной техники
Частный дом престарелых в Киеве: комфорт, забота и профессиональный уход
Есть ли какой нибудь способ отслеживать в программе действия garbage collector? например писать в логи все его похождениявот он запустился, прошелся...
В моем приложении есть RecyclerViewМне необходимо реализовать визуальное удаление элемента списка RecyclerView при нажатии на кнопку (Кнопка указана...
Создал бота Discord, с возможностью подключения к нему по сокетамКод для создания самого бота