a strange issue with spring hibernate. i'm using junit for test. When i try to do the criteria select, i get no result for the test(empty set) and then all my rows are deleted . Here is one of my dao:
here is the autogenerated entity Abonne:Code:public class AbonneDAO extends HibernateDaoSupport implements IAbonneDAO{ //private final static Logger log = (Logger) LoggerFactory.getLogger(AbonneDAO.class); //private SessionFactory sessionFactory; private List<Abonne> abonnes; public List<Abonne> getAbonnes() { return abonnes; } public void setAbonnes(List<Abonne> abonnes) { this.abonnes = abonnes; } @Override public void ajouter(Abonne abonne) { getHibernateTemplate().save(abonne); } @Override public void modifier(Abonne abonne) { getHibernateTemplate().update(abonne); } @Override public void rechercher(Abonne abonne) { Criteria query = getSession().createCriteria(Abonne.class); if ( abonne.getNomParution1() != null ) query.add(Expression.eq("nom_parution1", abonne.getNomParution1())); //and the same for other criterion abonnes= query.list(); } @Override public void supprimer(Abonne abonne) { getHibernateTemplate().delete(abonne); getHibernateTemplate().flush(); } }
and here is the application contextCode:@Entity @Table(name = "abonne", catalog = "annuaire", schema = "") @NamedQueries({@NamedQuery(name = "Abonne.findAll", query = "SELECT a FROM Abonne a"), @NamedQuery(name = "Abonne.findByIdAbonne", query = "SELECT a FROM Abonne a WHERE a.idAbonne = :idAbonne"), @NamedQuery(name = "Abonne.findByNomParution1", query = "SELECT a FROM Abonne a WHERE a.nomParution1 = :nomParution1"), @NamedQuery(name = "Abonne.findByNomParution2", query = "SELECT a FROM Abonne a WHERE a.nomParution2 = :nomParution2"), @NamedQuery(name = "Abonne.findByNab9", query = "SELECT a FROM Abonne a WHERE a.nab9 = :nab9"), @NamedQuery(name = "Abonne.findByNab3", query = "SELECT a FROM Abonne a WHERE a.nab3 = :nab3"), @NamedQuery(name = "Abonne.findByNab2", query = "SELECT a FROM Abonne a WHERE a.nab2 = :nab2"), @NamedQuery(name = "Abonne.findByCategorieClient", query = "SELECT a FROM Abonne a WHERE a.categorieClient = :categorieClient") public class Abonne implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "id_abonne") private Integer idAbonne; @Column(name = "nom_parution1") private String nomParution1; @Column(name = "nom_parution2") public Abonne() { } public Abonne(Integer idAbonne) { this.idAbonne = idAbonne; } public Integer getIdAbonne() { return idAbonne; } public void setIdAbonne(Integer idAbonne) { this.idAbonne = idAbonne; } public String getNomParution1() { return nomParution1; } public void setNomParution1(String nomParution1) { this.nomParution1 = nomParution1; } public String getNomParution2() { return nomParution2; } public void setNomParution2(String nomParution2) { this.nomParution2 = nomParution2; } @Override public int hashCode() { int hash = 0; hash += (idAbonne != null ? idAbonne.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Abonne)) { return false; } Abonne other = (Abonne) object; if ((this.idAbonne == null && other.idAbonne != null) || (this.idAbonne != null && !this.idAbonne.equals(other.idAbonne))) { return false; } return true; } @Override public String toString() { return "entity.Abonne[idAbonne=" + idAbonne + "]"; } }
last one is the hibernate confiiguration fileHTML Code:<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="jdbc.properties" /> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="configLocation"> <value>hibernate.cfg.xml</value> </property> <property name="configurationClass"> <value>org.hibernate.cfg.AnnotationConfiguration</value> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> </bean> <bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> <property name="jdbcExceptionTranslator"> <ref bean="jdbcExceptionTranslator" /> </property> </bean> <!-- implémentation de la couche [dao] --> <bean id="daoAbonne" class="dao.AbonneDAO"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate" /> </property> </bean> </beans>
I get also no error no warning after the test!HTML Code:<hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/annuaire</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="show_sql">true</property> <property name="hibernate.format_sql">true</property> <mapping class="entity.Abonne"/> <!-- andsome other classes --> </...>
Thkx in advance for any ideaCode:public class AbonneDAOTest extends TestCase { private ApplicationContext ctx = null; private IAbonneDAO dao = null; public AbonneDAOTest() { // Should put in a parent class that extends TestCase String[] paths = {"applicationContext.xml"}; ctx = new ClassPathXmlApplicationContext(paths); } protected void setUp() throws Exception { super.setUp(); dao = (IAbonneDAO) ctx.getBean ("daoAbonne"); } protected void tearDown() throws Exception { super.tearDown(); dao = null; } public void testFindRecord() throws Exception { Abonne ab=new Abonne; ab.setName("aaa"); List <Abonne> L = dao.rechercher(ab); }![]()


Reply With Quote
