Valentyn Grygoriev
Valentyn Grygoriev

Reputation: 463

Spring JPA repository doesn't return List of entities

I try to get simple List of Rawtype entities with help of findBy method in the myMethod. But I get nothing - rawtypes doesn't contain any entity. Although findAll method works fine. Please tell we where is my mistake.

Rawtype.java

@Entity
@Table(name="rawtype")
public class Rawtype implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="rtid", nullable = false)
    @GeneratedValue
    private int rtId;

    @Column(name="rtname", nullable = false)
    private String rtName;

    //getters and setters

RawtypeRepository.java

public interface RawtypeRepository extends JpaRepository<Rawtype, Integer> {
    List<Rawtype> findByRtName(String rtName);
}

RawtypeServiceImpl.java

@Service
@Transactional
public class RawtypeServiceImpl implements RawtypeService {
    @Autowired
    RawtypeRepository rawtypeRepository;

    public List<Rawtype> findAll() {
        return rawtypeRepository.findAll();
    }

    public myMethod(){
        List<Rawtype> rawtypes = rawtypeRepository.findByRtName("RawName");
    }
}

Upvotes: 3

Views: 4135

Answers (1)

Darshan Mehta
Darshan Mehta

Reputation: 30819

Can you try printing rtName of all the entities returned by findAll() method? May be there isn't any record with 'RawName' as rtName.

Also, you can enable logging for JPA to see the generated query.

Upvotes: 1

Related Questions