Reputation: 6989
I was working on a web project which making use of Spring MVC and JPA, and it was build using MAVEN. Somehow I wasn't sure whether I have the JPA configuration done correctly because I'm getting this error while launching the web
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [from User]
I have the table in MYSQL created in this way:
mysql> desc users;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO | PRI | | |
| password | varchar(32) | YES | | NULL | |
| enabled | int(11) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
I have the User entity declare in this way:
@Entity
@Table(name="users")
public class User {
@Id
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Column(name="enabled")
private Integer enabled;
I have this configure in datasource.xml
:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="org.huahsin.ProjectA.*" />
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQLInnoDBDialect" />
<property name="showSql" value="true" />
</bean>
And this is how the DAO is going to invoke the persistence object:
@Repository
public class UserDao implements IUserDao {
@PersistenceUnit
private EntityManagerFactory emf;
public User findByUsername(String username) {
List<User> l = (List<User>) emf.createEntityManager().createQuery("from User");
...
}
}
Upvotes: 1
Views: 3534
Reputation: 692231
packagesToScan
expects a list of package names. org.huahsin.ProjectA.*
is not a valid package name. Make sure to specify the package (or any super-package) of your entity in this property.
Upvotes: 3