我有一个springapp-dao.xml,如下所示
<?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>
然后我有一个springapp-service.xml,如下所示
<?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>
然后我有一个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();
}
}
然后我有一个正确映射到Contacts表的POJO
@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;
........
然后我有一个服务类
@Service
public class ContactServiceImpl implements ContactService {
private ContactDAO contactDAO;
public ContactDAO getContactDAO() {
return contactDAO;
}
public void setContactDAO(ContactDAO contactDAO) {
this.contactDAO = contactDAO;
}
....................
现在我有2个问题 首先,当我试图在ContactServiceImpl中的ContactsDAOImpl和ContactDAO中自动装配会话工厂时,它根本没有接线。 然后我使用了setter和getter,如上所示。然后它开始注入所需的依赖项。 现在我的问题是当我是ContactDAOImpl的listContact()方法时,(首先我必须在工厂调用open connection而不是getConnection,但这不起作用), 它抛出以下的例外情况
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: Contacts is not mapped [from Contacts]
我是否需要创建hbm fil
答案 0 :(得分:2)
您需要告诉LocalSessionFactoryBean您放置类的位置,因此您的Contacts类所在的包都是:
<property name="packagesToScan" value="your.orm.package"/>