hi guys this is my first application in with spring and hibernate.So please bear with me with stupid questions :). i created a simple java application in netbeans 6.7. here are my daos interfaces UsersDAO
package Dao;
import Entities.Users;
public interface UsersDAO {
public Long GetIdByUsernameAndPasswor(String username, String password);
public Users GetAllByID(Long id);
public boolean Create(Users user);
public boolean Delete(Users user);
public boolean Edit(Users user);
}
and the ContactDAO
package Dao;
import Entities.Contacts;
import java.util.List;
public interface ContactsDAO {
public List GetAll();
public Contacts GetAllById(Long Id);
public boolean Create(Contacts contact);
public boolean Delete(Contacts contact);
public boolean Edit(Contacts contact);
}
and their implementations
package Dao.DaoImpl;
import Dao.UsersDAO;
import Entities.Users;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class UserDAOImpl extends HibernateDaoSupport implements UsersDAO {
// private SessionFactory sessionFactory;
public UserDAOImpl(){}
public Long GetIdByUsernameAndPasswor(String username, String password)
{
try
{
return DataAccessUtils.longResult(getHibernateTemplate().find("select u.user_id from Users u where u.username=? and u.password", new Object[] {username, password}) );
}
catch(Exception ex)
{
ex.printStackTrace();
return Long.parseLong("0");
}
}
public Users GetAllByID(Long id) {
try
{
return (Users) getHibernateTemplate().get(Users.class, id);
}
catch(Exception ex)
{
ex.printStackTrace();
return new Users();
}
}
public boolean Create(Users user) {
try
{
getHibernateTemplate().save(user);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
public boolean Delete(Users user) {
try
{
getHibernateTemplate().delete(user);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
public boolean Edit(Users user) {
try
{
getHibernateTemplate().saveOrUpdate(user);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
}
package Dao.DaoImpl;
import Dao.ContactsDAO;
import Entities.Contacts;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class ContactsDAOImpl extends HibernateDaoSupport implements ContactsDAO{
public ContactsDAOImpl(){}
public List GetAll() {
try
{
return getHibernateTemplate().find("from Contacts");
}
catch(Exception ex)
{
ex.printStackTrace();
return null;
}
}
public Contacts GetAllById(Long Id) {
return (Contacts) getHibernateTemplate().get(Contacts.class, Id);
}
public boolean Create(Contacts contact) {
try
{
getHibernateTemplate().save(contact);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
public boolean Delete(Contacts contact) {
try
{
getHibernateTemplate().delete(contact);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
public boolean Edit(Contacts contact) {
try
{
getHibernateTemplate().saveOrUpdate(contact);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
}
my spring configuration file is under the folder Resources.so normally the path is Resouces/contactmanagement.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ContactsMan" />
<property name="username" value="root" />
<property name="password" value="letmein" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.SessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResouces">
<list>
<value>Resources/users.hbm.xml</value>
<value>Resources/contacts.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="usersdao" class="Dao.DaoImpl.UserDAOImpl">
<property name="sessionFactory" ref="sessionFactory">
</bean>
<bean id="contactsdao" class="Dao.DaoImpl.ContactDAOImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
mapping files are under the same Resources folder users.hbm.xml contacts.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Entities.Contacts" table="contacts">
<id name="contact_id">
<generator class="increment"/>
</id>
<many-to-one cascade="" class="Users" name="user"/>
<property name="firstname" />
<property name="lasstname" />
<property name="cellphone1" />
<property name="cellphone2" />
<property name="telephone" />
<property name="email" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Entities.Users" table="users">
<id name="users_id">
<generator class="increment"/>
</id>
<bag name="contacts" inverse="true" lazy="true">
<key column="user_id"/>
<one-to-many class="Contacts"/>
</bag>
<property name="username"/>
<property name="passsword"/>
<property name="city"/>
<property name="country"/>
</class>
</hibernate-mapping>
this is finally my main class
package main;
import Dao.UsersDAO;
import Entities.Users;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext
public class contactmanagement {
public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("Resources/contactmanagement.xml");
UsersDAO usersdao = (UsersDAO) ctx.getBean("usersdao");
Users user = new Users();
user.setUsername("me");
user.setPassword("mypass");
user.setCity("somecity");
user.setCountry("somecountry");
usersdao.Create(user);
System.out.println("created");
}
when i run this it said to give a summary "No bean named 'usersdao' is defined" Please what did i do wrong? here is another question about the DAOs implementation class. should i set the Property setSessionFactory? or spring handle every thing through the getHibernateTemplate() ? Please let me get Through this.Thanks for reading.I know it's long ;)