Question

I get the following error when i try to manipulate the person 'entity' from within the spring container:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.s
pring.entity.Person
        at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionF
actoryImpl.java:1141)
        at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.jav
a:1433)

Where am I going wrong - perhaps in trying to combine the xml and annotated metadata?
Appreciate any help on this.

Entity:

@Entity
@Table(name = "PERSON")
public class Person implements Serializable {

    private static final long serialVersionUID = -5527566248002296042L;

    @Id
    @Column(name = "ID")
    @GeneratedValue
    private Integer id;

    @Column(name = "FIRST_NAME")
    private String firstName;

    @Column(name = "LAST_NAME")
    private String lastName;
.....
.....
}

Service bean:

@Service("personService")
@Transactional
public class PersonService {


    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;

    public List<Person> getAll() {

        // Retrieve session from Hibernate
        Session session = sessionFactory.openSession();
        try{ 
        // Create a Hibernate query (HQL)
        Query query = session.createQuery("FROM  Person");

        // Retrieve all
        return  query.list();
        }
        finally{
        session.close();
        }
    }
....
....
}

Main:

public static void main(String[] args){

        ApplicationContext appContext = 
                new ClassPathXmlApplicationContext("META-INF/beans-txn.xml");


        PersonService personService = (PersonService)appContext.getBean("personService");
        personService.add("Rob","Cahill", new Double(20000));
        List<Person> persons = personService.getAll();
    .....

spring configuration:

<context:annotation-config />

    <bean id="personService" class="org.spring.service.PersonService"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        .....
    </bean>

        <!-- Hibernate session factory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="hibernateProperties">
        .....
        </property>

    </bean>
Was it helpful?

Solution

Add to your sessionFactory bean definition:

<property name="packagesToScan" value="common.**.entities" />

Where common.**.entities - package with your entities.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top