3

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");
    }
}
2
  • Why not write a test that adds an entity with rtName set to "RawName" then tries to findByRtName()? BTW, I don't think it needs @Transactional, your operations are currently read-only. Commented Mar 14, 2016 at 1:37
  • @todd-w-crone Thank you for your answer. I'm going to add some CRUD methods in the RawtypeServiceImpl(). Consequently I will need in that annotation. Commented Mar 14, 2016 at 9:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. It was a mistake in importing data from Excel to MySql. This import added some non visual symbols like "\n". As the result findByRtName return wrong values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.