1

If I don't write a custom query the @Autowired FirstnameRepository object is null, therefore I tried to write a custom query, but this produces the following error.

java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List FirstnameRepository.findByNation(Nation)!

Since the query looks correct to me, I think it has something to do with my classstructure or incorrect annotations.

FirstnameRepo

 public interface FirstnameRepository extends JpaRepository<Firstname, String>{

    @Query("SELECT fn FROM Firstname fn WHERE fn.nation = :nation")
    List<Firstname> findByNation(@Param("nation")Nation nation);
}

Firstname Model

@Entity
@Table(name = "Firstnames")
public class Firstname extends Name implements Serializable {
    private static final long serialVersionUID = 1L;

    @Basic(optional = false)
    @Column(name = "gender")
    private short gender;
    @JoinColumn(name = "nation", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Nation nation;

    public Firstname() {}      
}

Since there is also a Lastname model class I extend a class named Name to save Firstname and Lastname in the same Map. Name has no table in the database and both classes only inherit the ID property.

Name class

@MappedSuperclass
public abstract class Name {
    @Id
    @Basic(optional = false)
    @Column(name = "name")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

If I comment the findByNation() method out, the server starts without a problem. I hope this is all you need, the server configuration is more or less copied from a working project, but if I should include something let me know. thanks

EDIT The Problem was a incorrect configuration. I changed alot of stuff before testing it again, but seems like the main issue was a wrong version of in the web.xml.

KenavR
  • 3,769
  • 9
  • 37
  • 47
  • should'nt it be `SELECT fn FROM Firstname fn WHERE fn.nation in :nation`, ie `in` operator for collection – Ankur Singhal Sep 03 '14 at 09:50
  • It is one specific nation in Firstname, it's the "One" side of the relationship, therefore I don't think so. But I will try it. => Same error. – KenavR Sep 03 '14 at 09:54
  • This propably won't help with your issue, but shouldn't Name class be Serializable as well? – Sami Korhonen Sep 03 '14 at 10:23

2 Answers2

2

Even though this problem has most likely something to do with the Query itself, in my case was a configuration issue. Here are some things I checked

  • Version of in web.xml which specifies the Version of the used Servlet specification - SO Question
  • Make sure your database context (e.g. db-context.xml) gets loaded
Community
  • 1
  • 1
KenavR
  • 3,769
  • 9
  • 37
  • 47
0

Your query is malformed... you can't put this "fn.nation = :nation" where Nation is an Entity, you should use a join between tables to work fine.

I suggest you this link.

Xstian
  • 8,184
  • 10
  • 42
  • 72
  • I thought about that, but I generated some Modelclasses in Netbeans and it creates Queries like this "SELECT f FROM Firstname f WHERE f.nation.region = :region". In this case Region is also an Entity and there is no join for nation either. But I will try it with a JOIN. – KenavR Sep 03 '14 at 10:10
  • @KenavR Let me know, if does not works fine, I try to help you in another way. – Xstian Sep 03 '14 at 10:15
  • No difference. I tried it with Objectcomparison and Id comparison, I guess the error doesn't have anything to do with the Query itself. I will check my configuration again. – KenavR Sep 03 '14 at 10:25
  • @KenavR see [here](http://stackoverflow.com/questions/19733877/jpql-join-queries), more or less is the same error. But in any case check your configuration. – Xstian Sep 03 '14 at 10:28
  • 1
    It was indeed a configuration issue, my web.xml had the wrong and I also changed some other stuff. – KenavR Sep 03 '14 at 11:25