问题
I am getting following exception
org.hibernate.MappingException: Unknown entity: com.sample.Student
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
回答1:
You are trying to mix two things
Annotations
and
LocalSessionFactoryBean
use the child class of LocalSessionFactoryBean that is
AnnotationSessionFactoryBean
replace this
LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
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
回答2:
Replace the LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
with the following
AnnotationSessionFactoryBean annotationSessionFactoryBean = new AnnotationSessionFactoryBean();
annotationSessionFactoryBean.setPackagesToScan(new String[]{"com.sample"});
来源:https://stackoverflow.com/questions/18629702/org-hibernate-mappingexception-unknown-entity-in-spring-orm