0

I have an issue with my project using Spring (4.2.4 Release) and JPA (2.1)

To be brief the problem is in the part of code in file "Book.java":

@Table(name = "book")
@NamedQueries({
        @NamedQuery(name = "Book.getBooks", query="select b from com.jpaProSpring.Book b ")
})
@Entity(name = "Book")
public class Book implements Serializable {
    private int id;
    private String title;
    private String description;

    public Book() {

    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id")
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Column
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
} 

and particulary in

query = "select b from com.jpaProSpring.Book b" )

My IntelliJ highlights the Book and says that class is not en entity. And I have no idea, why it is so given that I was doing an example from the book.

Link to my project https://github.com/yuraguz/LearnORM.git

My spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/jdbc
              http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
              http://www.springframework.org/schema/data/jpa
              http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd
       ">


        <tx:annotation-driven transaction-manager="transactionManager" />

       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
              <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
              <property name="url" value="jdbc:mysql://localhost:3310/testdb"/>
              <property name="username" value="root"/>
              <property name="password" value="1234"/>
              <property name="initialSize" value="5"/>
              <property name="maxActive" value="10"/>
       </bean>

       <bean id="transactionManager"
             class="org.springframework.orm.jpa.JpaTransactionManager">
              <property name="entityManagerFactory" ref="emf" />
       </bean>

       <bean id="emf"
             class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
              <property name="dataSource" ref="dataSource" />
              <property name="jpaVendorAdapter">
                  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
              </property>
              <property name="packagesToScan" value="com.jpaProSpring" />
              <property name="jpaProperties">
                  <props>
                      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                      <prop key="hibernate.max_fetch_depth">3</prop>
                      <prop key="hibernate.jdbc.fetch_size">50</prop>
                      <prop key="hibernate.jdbc.batch_size">10</prop>
                      <prop key="hibernate.show_sql">true</prop>
                  </props>
              </property>
       </bean>

         <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
         <context:annotation-config />
         <context:component-scan base-package="com.jpaProSpring" />
</beans>

P.S: I know entities must be declared in persistence.xml in META-INF/ source. But if I understand correctly Spring4 make it possible to config project without persistance.xml. So, I don't use it.

Ken Bekov
  • 13,696
  • 3
  • 36
  • 44
Yury
  • 3
  • 4
  • Post the entire file for the class. – chrylis -cautiouslyoptimistic- Dec 24 '15 at 13:27
  • Does it run fine in your tests? Because IDEs try to help, but sometimes they aren't right, or they aren't configured correctly enough to give correct messages. – JB Nizet Dec 24 '15 at 13:49
  • I guess this might be helpful, http://stackoverflow.com/questions/21381943/how-to-configure-spring-without-persistence-xml – Matrix Buster Dec 24 '15 at 14:22
  • @chrylis I've done it) – Yury Dec 24 '15 at 15:59
  • @JBNizet the fact is that i don't use tests for this project :) – Yury Dec 24 '15 at 15:59
  • @MatrixBuster There is a good information about my problem, but after doing those recomendations I've got another message from compiler: **No query defined for that name [Book.getBooks]** (and old problem persists) – Yury Dec 24 '15 at 15:59
  • Your entity is named `Book` not `com.jpaProSpring.Book`. Basically your query is using the wrong name of the entity. – M. Deinum Dec 24 '15 at 17:41
  • I checked your code. It works fine. Is your problem just your IDE dose not recognize entities? Does your program runs properly at runtime? – Ken Bekov Dec 25 '15 at 14:13
  • @M.Deinum if i use name "Book" he even doesn't find the entity Book (IDE highlights this name **"Book"** and say, that he cannot resolve this type..) So, when y use this name **"com.jpaProSpring.Book"**, my IDE find this clas Book, but says that Book is not defined an entity – Yury Dec 26 '15 at 12:06
  • The fact that your IDE says that doesn't mean it is wrong (or that your IDE is right). Trust me the name is `Book` and nothing else. – M. Deinum Dec 26 '15 at 14:20

1 Answers1

2

If you use InellyJ Idea, possible you didn't add JPA facet in your project. Try open menu "File"->"Project Structure", then select in list of Project Settings section named "Facets". Check if you have configured JPA facet in list of facets. If you don't see JPA facet, just add it (by pressing "+" button). Optionally, you can specify persistence.xml (if you have it) and Default JPA provider. Hope this will help.

Ken Bekov
  • 13,696
  • 3
  • 36
  • 44