commit sous hibernate + spring
Voila, dernière question... tout marche ou presque.
j'ai un application context comme ca :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
<property name="url"><value>jdbc:hsqldb:/home/yannmauron/toxico_db</value></property>
<property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>com/genebio/toxico/compound/CompoundImpl.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.connection.autocommit">true</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="CompoundDAO" class="com.genebio.toxico.compound.CompoundDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
</beans> |
un mapping comme ca :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.genebio.toxico.compound" schema="PUBLIC">
<class table="COMPOUND" name="CompoundImpl">
<id name="id" type="int" column="COMPOUND_ID"></id>
<property name="cid" type="string" column="COMPOUND_CID" />
<property name="SMILE" type="string" column="COMPOUND_SMILE" />
</class>
</hibernate-mapping> |
mon objet qui est :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
package com.genebio.toxico.compound;
/**
* Basic class for the compound object
*
* @author Yann
*
*/
import java.util.ArrayList;
import java.io.Serializable;
public class CompoundImpl extends CompoundImplBase implements Serializable{
public CompoundImpl() {
}
protected String cid;
protected int id;
protected String SMILE;
protected ArrayList<CompoundDescriptor> compoundDescriptors;
protected ArrayList<CompoundName> compoundNames;
protected ArrayList<CompoundClassification> compoundclassification;
protected ArrayList<CompoundCorrelation> compoundcorrelations;
protected ArrayList<CompoundXref> compoundXrefs;
//minimum information for a compound
public CompoundImpl(int id, String cid, String SMILE){
this.id = id;
this.cid = cid;
this.SMILE = SMILE;
}
//primary information for a compound
public CompoundImpl(int id, String cid, String SMILE,
ArrayList<CompoundDescriptor> compoundDescriptors,
ArrayList<CompoundName> compoundNames,
ArrayList<CompoundXref> compoundXrefs
){
this.id = id;
this.cid = cid;
this.SMILE = SMILE;
this.compoundDescriptors = compoundDescriptors;
this.compoundNames = compoundNames;
this.compoundXrefs = compoundXrefs;
}
//future information for a compound (correlation and classification informations are added)
public CompoundImpl(int id, String cid, String SMILE,
ArrayList<CompoundDescriptor> compoundDescriptors,
ArrayList<CompoundName> compoundNames,
ArrayList<CompoundXref> compoundXrefs,
ArrayList<CompoundClassification> compoundclassification,
ArrayList<CompoundCorrelation> compoundcorrelations
){
this.id = id;
this.cid = cid;
this.SMILE = SMILE;
this.compoundclassification = compoundclassification;
this.compoundcorrelations = compoundcorrelations;
this.compoundDescriptors = compoundDescriptors;
this.compoundNames = compoundNames;
this.compoundXrefs = compoundXrefs;
}
public String getSMILE(){
return SMILE;
}
public void setSMILE(String SMILE){
this.SMILE=SMILE;
}
public ArrayList<CompoundClassification> getCompoundclassification() {
return compoundclassification;
}
public void setCompoundclassification(
ArrayList<CompoundClassification> compoundclassification) {
this.compoundclassification = compoundclassification;
}
public ArrayList<CompoundCorrelation> getCompoundcorrelations() {
return compoundcorrelations;
}
public void setCompoundcorrelations(
ArrayList<CompoundCorrelation> compoundcorrelations) {
this.compoundcorrelations = compoundcorrelations;
}
public ArrayList<CompoundDescriptor> getCompoundDescriptors() {
return compoundDescriptors;
}
public void setCompoundDescriptors(
ArrayList<CompoundDescriptor> compoundDescriptors) {
this.compoundDescriptors = compoundDescriptors;
}
public ArrayList<CompoundName> getCompoundNames() {
return compoundNames;
}
public void setCompoundNames(ArrayList<CompoundName> compoundNames) {
this.compoundNames = compoundNames;
}
public ArrayList<CompoundXref> getCompoundXrefs() {
return compoundXrefs;
}
public void setCompoundXrefs(ArrayList<CompoundXref> compoundXrefs) {
this.compoundXrefs = compoundXrefs;
}
public int getId() {
return id;
}
public String getCid() {
return cid;
}
public void setId(int id) {
this.id=id;
}
public void setCid(String cid) {
this.cid=cid;
}
} |
et enfin l'implémentation de mon DAO :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
package com.genebio.toxico.compound;
import java.util.List;
import com.genebio.toxico.compound.Compound;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class CompoundDAOImpl extends HibernateDaoSupport implements CompoundDAO{
public void saveCompound(CompoundImpl compound) {
getHibernateTemplate().saveOrUpdate(compound);
}
public CompoundImpl getCompound(int id) {
return (CompoundImpl) getHibernateTemplate().get(CompoundImpl.class, id);
}
public void removeCompound(int id) {
Object compound = getHibernateTemplate().load(Compound.class, id);
getHibernateTemplate().delete(compound);
}
} |
je fais le junit test comme ca :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
package com.genebio.toxico.compound;
import static org.junit.Assert.assertEquals;
import org.hibernate.classic.Session;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CompoundImplDAOTest {
private CompoundImpl compoundImpl = null;
private CompoundDAOImpl compoundDAO = null;
private ApplicationContext ctx = null;
@Before
public void setUp() throws Exception {
String[] paths = {"applicationContext.xml"};
ctx = new ClassPathXmlApplicationContext(paths);
compoundDAO = (CompoundDAOImpl) ctx.getBean("CompoundDAO");
}
@Test
public void testSaveRecord() throws Exception {
compoundImpl = new CompoundImpl();
compoundImpl.setId(3);
compoundImpl.setCid("1234567");
compoundImpl.setSMILE("lkjfdg");
compoundDAO.saveCompound(compoundImpl);
Assert.assertNotNull("primary key assigned", compoundImpl.getId());
}
@Test
public void testloadRecord() throws Exception {
CompoundImpl compound = compoundDAO.getCompound(2);
System.out.println(compound.toString());
}
} |
Et du coup tout passe (j'ai mis une entrés a la main avec une id 2 dans la base et il la lit bien), mais le test testSaveRecord() n'enregistre rien dans la base :-( Une idée?