本文介绍了org.hibernate.MappingException:未知实体:在spring orm中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到以下异常:
我在Stackoverflow上看到了很多同样问题的答案,但是他们都建议使用来自javax.persistence的@Entity批注而不是hibernate,在我的例子中
我的POJO课程
我只是从javax.persistence中使用它, package com.sample;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Student {
@Id
int id;
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
字符串firstName;
字符串姓氏;
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
$ / code $ / pre
和我的Manger class
package com.sample;
import java.util.Properties;
import org.hibernate.SessionFactory;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
public class ORMManager {
/ **
* @param args
* /
public static void main(String [] args ){
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setUrl(jdbc:oracle:thin:@localhost:1521:XE);
ds.setDriverClassName(oracle.jdbc.driver.OracleDriver);
ds.setUsername(system);
ds.setPassword(tiger);
LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
lsfb.setDataSource(ds);
HibernateTemplate template = new HibernateTemplate();
属性prop = new Properties();
prop.put(hibernate.dialect,org.hibernate.dialect.OracleDialect);
prop.put(hibernate.show_sql,true);
prop.put(hbm2ddl.auto,create);
lsfb.setHibernateProperties(prop);
尝试{
lsfb.afterPropertiesSet();
} catch(Exception e){
e.printStackTrace();
}
template.setSessionFactory((SessionFactory)lsfb.getObject());
template.afterPropertiesSet();
学生s =新生Student();
s.setFirstName(pallavi);
s.setLastName(sing);
template.save(s);
System.out.println(done);
$ b
请帮我解决这个问题
解决方案您正在尝试混合两件事
和
使用LocalSessionFactoryBean的子类
取代此
with
AnnotationSessionFactoryBean lsfb = new AnnotationSessionFactoryBean();
Class [] annotatedClasses = {Student.class};
lsfb.setAnnotatedClasses(annotatedClasses);
或
AnnotationSessionFactoryBean lsfb = new AnnotationSessionFactoryBean();
annotationSessionFactoryBean.setPackagesToScan(new String [] {com.sample});
看到这个
I am getting following exception
I have seen so many answers to same question on Stackoverflow but all of them suggests to use @Entity annotation from javax.persistence instead of hibernate, in my case I am using it from javax.persistence only but still getting this exception.
my POJO class
package com.sample;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Student {
@Id
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
String firstName;
String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
and my Manger class
package com.sample;
import java.util.Properties;
import org.hibernate.SessionFactory;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
public class ORMManager {
/**
* @param args
*/
public static void main(String[] args) {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setUrl("jdbc:oracle:thin:@localhost:1521:XE");
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUsername("system");
ds.setPassword("tiger");
LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
lsfb.setDataSource(ds);
HibernateTemplate template = new HibernateTemplate();
Properties prop = new Properties();
prop.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
prop.put("hibernate.show_sql", "true");
prop.put("hbm2ddl.auto", "create");
lsfb.setHibernateProperties(prop);
try {
lsfb.afterPropertiesSet();
} catch (Exception e) {
e.printStackTrace();
}
template.setSessionFactory((SessionFactory)lsfb.getObject());
template.afterPropertiesSet();
Student s = new Student();
s.setFirstName("pallavi");
s.setLastName("sing");
template.save(s);
System.out.println("done");
}
}
please help me resolve this issue
解决方案 You are trying to mix two things
and
use the child class of LocalSessionFactoryBean that is
replace this
with
AnnotationSessionFactoryBean lsfb = new AnnotationSessionFactoryBean();
Class [] annotatedClasses = {Student.class};
lsfb.setAnnotatedClasses(annotatedClasses);
or
AnnotationSessionFactoryBean lsfb = new AnnotationSessionFactoryBean();
annotationSessionFactoryBean.setPackagesToScan(new String[]{"com.sample"});
see this answer
这篇关于org.hibernate.MappingException:未知实体:在spring orm中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!