DNAj
DNAj

Reputation: 270

how to configure hibernate 4 with spring 3.1

I have one springapp-dao.xml which looks like below

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">

    <jee:jndi-lookup id="masterDS" jndi-name="java:jboss/datasources/MySqlDS" />
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="masterDS" />
        <property name="hibernateProperties">
        <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        </props>
</property>
    </bean>
</beans>

Then i have one springapp-service.xml which is as follow

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="masterDS" />
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(* service.*.*(..))"
            id="service" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="service" />
    </aop:config>
    <bean id="contactDAO" class="dao.ContactDAOImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="contactService" class="service.ContactServiceImpl">
    <property name="contactDAO" ref="contactDAO"/>
    </bean>
</beans>

Then i have one DAOImpl

@Repository("ContactDAO")
@Transactional(readOnly = false)
public class ContactDAOImpl  implements ContactDAO {
private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }



public List<Contacts> listContact() {

            return sessionFactory.openSession().createQuery("from Contacts")
                    .list();
        }

}

Then i have one POJO which correctly maps to Contacts table

@Entity
@Table(name = "CONTACTS")
public class Contacts {

    @Id
    @Column(name = "ID")
    @GeneratedValue
    // The @GeneratedValue annotation says that this value will be determined by
    // the datasource, not by the code.
    private Integer id;

    @Column(name = "FIRSTNAME")
    private String firstname;

    @Column(name = "LASTNAME")
    private String lastname;

    @Column(name = "EMAIL")
    private String email;

    @Column(name = "TELEPHONE")
    private String telephone;
    @Column(name = "CREATED")
    private String created;

........

Then i have one service class

@Service
public class ContactServiceImpl implements ContactService {


    private ContactDAO contactDAO;

    public ContactDAO getContactDAO() {
        return contactDAO;
    }

    public void setContactDAO(ContactDAO contactDAO) {
        this.contactDAO = contactDAO;
    }

....................

Now i have 2 questions Firstly when i was trying to autowire session factory in ContactsDAOImpl and ContactDAO in ContactServiceImpl , it wasnt getting wired at all. Then i used setter and getters as you can see above. Then it started injecting required dependencies. Now my problem is when i am listContact() method of ContactDAOImpl, (firstly i had to call open connection on the factory instead of getConnection but that doesn work), it throws following exceptiopn

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: Contacts is not mapped [from Contacts]

Do i need to create hbm fil

Upvotes: 0

Views: 2136

Answers (1)

Jeff
Jeff

Reputation: 21

You need to tell the LocalSessionFactoryBean where you put your classes, so whatever package your Contacts class is in:

<property name="packagesToScan" value="your.orm.package"/>

Upvotes: 2

Related Questions