本文介绍了`packagesToScan`不适用于@Entity和@NamedQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的User.java类
package com.sudosmith.cenmesser.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
$ b $ **
*
* @author Vishal Joshi
*
* /
@ Entity
@Table(name =User)
@NamedQuery(name =findUserByName,query =SELECT user FROM User user WHERE user.userName LIKE:queryString)
@ org.hibernate.annotations。实体(dynamicInsert = true,dynamicUpdate = true)
public class User实现Serializable {
$ b $ / **
*
* /
private static final long serialVersionUID = 7257051297399186919L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name =username)
private String userName;
@Column(name =password)
private String password;
$ b @ManyToMany
@JoinTable(name =user_role,joinColumns = @JoinColumn(name =userid),inverseJoinColumns = @JoinColumn(name =roleid))
私人列表< Role>角色;
@Enumerated(EnumType.STRING)
@Column(name =status)
私有UserStatus状态;
public User(){
}
public User(int id,String userName,String password,List< Role> roles,
UserStatus状态){
super();
this.id = id;
this.userName = userName;
this.password =密码;
this.roles =角色;
this.status = status;
$ b $ **
* @return the id
* /
public int getId(){
return id;
$ b $ ** b $ b * @param id
*要设置的ID
* /
public void setId(int id) {
this.id = id;
$ b / **
* @return userName
* /
public String getUserName(){
return userName;
}
$ b / **
* @param userName
*用户名设置
* /
public void setUserName(String userName) {
this.userName = userName;
$ b / **
* @返回密码
* /
public String getPassword(){
return password;
}
$ b / **
* @param密码
*密码设置
* /
public void setPassword(String password) {
this.password = password;
}
/ **
* @返回角色
* /
public List< Role> getRoles(){
if(roles!= null){
返回角色;
} else {
返回新的ArrayList< Role>();
}
}
/ **
* @param角色
*设置角色
* /
public void setRoles(列出<角色>角色){
this.roles =角色;
$ b / **
* @返回状态
* /
public UserStatus getStatus(){
return status;
}
$ b $ **
* @param status
*要设置的状态
* /
public void setStatus(UserStatus status) {
this.status = status;
}
}
在我的 spring-servlet.xml 我已经使用 packagesToScan 属性来映射所有具有 @Entity
< bean id =sessionFactoryclass =org.springframework.orm.hibernate3。 annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> $ {jdbc.dialect}< / prop>
< prop key =hibernate.show_sql> true< / prop>
< /道具>
< / property> < property name =packagesToScan>
< list>
<值> com.sudosmith.cenmesser.model。**< /值>
< / list>
< / property>
< / bean>
但是在添加此属性后,它不起作用。它不映射 @Entity 和 @NamedQuery 。我遇到了错误,如 mappingException:unknown @NamedQuery 。
$ b 编辑:Hibernate依赖关系
<依赖关系>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> 3.6.7.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-validator< / artifactId>
< version> 4.3.0.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-annotations< / artifactId>
< version> 3.3.1.GA< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 4.2.11< / version>
< /依赖关系>
解决方案
使用<值> com.sudosmith.cenmesser.model< /值>
This is my User.java class
package com.sudosmith.cenmesser.model; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.NamedQuery; import javax.persistence.Table; /** * * @author Vishal Joshi * */ @Entity @Table(name = "User") @NamedQuery(name = "findUserByName", query = "SELECT user FROM User user WHERE user.userName LIKE :queryString") @org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true) public class User implements Serializable { /** * */ private static final long serialVersionUID = 7257051297399186919L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "username") private String userName; @Column(name = "password") private String password; @ManyToMany @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "userid"), inverseJoinColumns = @JoinColumn(name = "roleid")) private List<Role> roles; @Enumerated(EnumType.STRING) @Column(name = "status") private UserStatus status; public User() { } public User(int id, String userName, String password, List<Role> roles, UserStatus status) { super(); this.id = id; this.userName = userName; this.password = password; this.roles = roles; this.status = status; } /** * @return the id */ public int getId() { return id; } /** * @param id * the id to set */ public void setId(int id) { this.id = id; } /** * @return the userName */ public String getUserName() { return userName; } /** * @param userName * the userName to set */ public void setUserName(String userName) { this.userName = userName; } /** * @return the password */ public String getPassword() { return password; } /** * @param password * the password to set */ public void setPassword(String password) { this.password = password; } /** * @return the roles */ public List<Role> getRoles() { if (roles != null) { return roles; } else { return new ArrayList<Role>(); } } /** * @param roles * the roles to set */ public void setRoles(List<Role> roles) { this.roles = roles; } /** * @return the status */ public UserStatus getStatus() { return status; } /** * @param status * the status to set */ public void setStatus(UserStatus status) { this.status = status; } }
in my spring-servlet.xml I have used the packagesToScan property to map all the model classes which has @Entity annotation.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${jdbc.dialect}</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.sudosmith.cenmesser.model.**</value> </list> </property> </bean>
But after adding this property its not working. Its not mapping @Entity and @NamedQuery. I am getting error like mappingException: unknown @NamedQuery.
EDIT: Hibernate Dependencies
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.6.7.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.3.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.3.1.GA</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.11</version> </dependency>
解决方案
Try it with <value>com.sudosmith.cenmesser.model</value>
这篇关于`packagesToScan`不适用于@Entity和@NamedQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!