2

I am using Spring Data JPA:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.7.1.RELEASE</version>
</dependency>

with Spring 4.3.7.RELEASE and Hibernate 5.2.9.Final.

When I query using findAll, the List returns contains null values.

Entity:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Entity
public class Etudiant implements Serializable {

    /**
     * Serial version UID
     */
    private static final long serialVersionUID = -1982480763983112005L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "idEtudiant")
    private Integer idEtudiant;

    @Column(name = "nomEtudiant")
    private String nomEtudiant;

    @Column(name = "prenomEtudiant")
    private String prenomEtudiant;

    @Column(name = "adresse")
    private String adresse;

    @Column(name = "dateNaissance")
    private Date dateNaissance;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "etudiant_cours", joinColumns = @JoinColumn(name = "idEtudiant", referencedColumnName = "idEtudiant"), inverseJoinColumns = 
    @JoinColumn(name = "idCours", referencedColumnName = "idCours"))
    private List<Cours> cours;
}

NB: Note that the problem is not from lombock, I had tested with getters and setters.

Repository:

@Repository
public interface EtudiantRepository extends JpaRepository<Etudiant, Integer> {     
}

Service:

@Service
public class EtudiantServiceImpl {

    @Autowired
    EtudiantRepository etudiantRepository;

    List<Etudiant> lst = new ArrayList<Etudiant>();

    public List<Etudiant> getAllEtudiant() {
        lst =  this.etudiantRepository.findAll();
        return lst;
    }
}

Debug result

5
  • What is your @Entity class? (Etudiant) Commented Aug 8, 2017 at 17:51
  • Yes, Entity is Etudiant Commented Aug 9, 2017 at 8:03
  • There is something missing or wrong in your question, but I can´t figure out what it is?!? Is the List null or does it contain null values? What for do You need the member lst in the Repository? Why don´t You show us the sourcecode for your Entity Etundiant? How do you start the debugging-session and when do You take the snapshot? Commented Aug 9, 2017 at 9:01
  • Question is updated. Commented Aug 9, 2017 at 9:19
  • The title of the question is misleading in that it mentions that the repository returns null, whereas it is the repository instance itself that is null. Commented Aug 9, 2017 at 10:58

4 Answers 4

3

In debug screenshot, it can be seen that etudiantRepository is null. Maybe you are missing @EnableJpaRepositories annotation in your Configuration.

EtudiantServiceImpl is being instantiated using Dependency Injection or with the new keyword?

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

2 Comments

Instantiated using Dependency Injection in the Controller
Are you debugging in a container or using something like junit?
1

Finally I found the solution :

change spring version from 4.3.7.RELEASE to 4.3.10.RELEASE

Comments

0

you need to use context:component-scan annotation into xml configuration for scanning base package and repository package, you can find code below :

<jpa:repositories base-package="com.demo.test.repository" />
<context:component-scan annotation-config="true"
        base-package="com.demo.test" />

and if findall() return null value that means table dose not having data, it is normal behavior. and also check your datasource and entity manager connection

1 Comment

I already add this configuration to applicationContext.xml, also it create database table from Entity class.
0

For me, it was an error while creating composite key IdClass named classNamePK.

@IdClass(className.class) // wrong
@IdClass(classNamePK.class) // right

Comments

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.