views:

28

answers:

2

Hi i am developing a spring mvc app thats using hibernate to connect to a mysql database that stores files.

I have two methods. one that adds all files from a specific file path of my choosing and another method that invokes a query to return me a list of the files stored from mysql.

The issue is this. When i execute the first method on its own ie populating the database, it works fine i can see the contents of that table from mysql command line. however, when i then execute the query method right after populating it, the contents of that said table is completely gone instantly. Its as if hibernate only stored the data in the mysql temporarily or somewhere in mysql, it deleted data imediatly and doesnt keep it their.

this is the method that populated the table:

/**
     * Test Method: ideal for another class to do this kind of work and this
     * pass the FileObject into this class
     */
    public void addSomeFiles() {
        System.out.println("addSomeFiles");
        File dir = new File(picturesPath);
        String[] fileNames = dir.list();

        for (int i = 0; i < fileNames.length; i++) {

            System.out.println(fileNames[i]);
            File file = new File(picturesPath + "\\" + fileNames[i]);
            if (file.isFile()) {
                FileObject fileO = contstructFileObject(file);
                if (fileO == null) {
                    System.out.println("fileO is null!!!!!");
                } else {
                    // addFile(fileO);

                    dbFileHelper.addFile(fileO);
                }
            }

        }

        System.out.println("//////////////");
        // File file;

    }

.........Hibernate template class........

public class DbFileHelper implements DbFileWrapper {


    private HibernateTemplate hbTemplate;
    //private static final String SQL_GET_FILE_LIST = "select filename, size, id, type from fileobject";
    private static final String SQL_GET_FILE_LIST = "select new FileObject(filename, size, id, type) from FileObject";

    public DbFileHelper() {

    }

    public void setHbTemplate(HibernateTemplate hbTemplate) {
        System.out.println("setHbTemplate");
        System.out.println("///////////////////");
        System.out.println("///////////////////");
        System.out.println("///////////////////");

        this.hbTemplate = hbTemplate;
    }





    // ////////////////////////////////////////////////

    @Override
    public String addFile(FileObject file) {
        // TODO Auto-generated method stub
        System.out.println("addFile using hibernate");

        if (hbTemplate == null) {
            System.out.println("hbTemplate is fucking null!! why?");
        }
        hbTemplate.saveOrUpdate(file);
        hbTemplate.flush();

        return "added succesfuly";
    }

And here is the other method that makes the query:

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

public JSONArray getFileList(String type){

    return constructJsonArray(dbFileHelper.getFileList(ALL));
}

private JSONArray constructJsonArray(List<FileObject> fileList ){

    JSONArray mJsonArray = new JSONArray();

    for (int i = 0; i < fileList.size(); i++) {
        System.out.println("fileName = " + fileList.get(i).getFilename() );
        //mJson.put("Filename", fileList.get(i).getFileName() );

        mJsonArray.add( new JSONObject().put("File ID", fileList.get(i).getId() ));
        mJsonArray.add( new JSONObject().put("Filename", fileList.get(i).getFilename() ));
        mJsonArray.add( new JSONObject().put("File type", fileList.get(i).getType()));
        mJsonArray.add( new JSONObject().put("File Size", fileList.get(i).getSize()));
    }

    return mJsonArray;
}

..........hibernate Template class.......

private static final String SQL_GET_FILE_LIST = "select new FileObject(filename, size, id, type) from FileObject";

@Override
public List<FileObject> getFileList(String type) {
    // TODO Auto-generated method stub
    List<FileObject> files = hbTemplate.find(SQL_GET_FILE_LIST);
    //hbTemplate.flush();
    return files;
}

..........

Finally here is a print screen of what i originaly put inside my table but dissapears on its own:

http://img411.imageshack.us/img411/9553/filelisti.jpg

Am i missing something here?

edit: additional info.

my hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;

<hibernate-mapping package="com.kc.models.FileObject" >

    <class name="com.kc.models.FileObject" table="fileobject">
        <id name="id" column="ID">
            <generator class="native" />
        </id>
        <property name="filename" type="string" column="FILENAME" />
        <property name="type" type="string" column="TYPE" />
        <property name="size" type="double" column="SIZE" />
        <property name="file" type="blob" length="1000000000" column="FILE" />
    </class> 

</hibernate-mapping> 

my controller:

@Override
public ModelAndView handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // TODO call a method that returns a list of Mobile Apps.


    testAddingSomeFilesToDb();
    return new ModelAndView("" + "testJsonResponse", "jsonArray",
            getFileList() );

}


private void testAddingSomeFilesToDb() {
    ctx = new ClassPathXmlApplicationContext("zang-file-service.xml");
    FileHelper file = (FileHelper) ctx.getBean("fileHelper");
    file.addSomeFiles();
}

/**
 * Get file list from sql server based on type
 * @return file list in json
 */ 
private JSONArray getFileList() {
    // TODO: Get request parameter that states what type of file extensions
    // the client wants to recieve

    ctx = new ClassPathXmlApplicationContext("zang-file-service.xml");
    FileHelper file = (FileHelper) ctx.getBean("fileHelper");

    return file.getFileList("all");
}

Another edit:

my .xml file configuring the session factory and hibernate template

<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-2.5.xsd
       http://www.springframework.org/schema/jee
       http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"&gt;

    <!-- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd -->

    <!-- Config properties files -->



    <!-- Hibernate database stuff -->



    <!-- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="locations"> <list> <value>/properties/jdbc.properties</value> 
        </list> </property> </bean> -->


    <!-- <bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="${database.driver}" /> <property 
        name="url" value="${database.url}" /> <property name="username" value="${database.user}" 
        /> <property name="password" value="${database.password}" /> </bean> -->


    <bean id="dataSource1"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/zangshop" />
        <property name="username" value="root" />
        <property name="password" value="password" />

    </bean>


    <!-- LocalSessionFactoryBean u need to put the hbm files in the WEB-INF/classes 
        root director -->

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource1"></property>
        <property name="mappingResources">
            <list>
                <value>FileObject.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>


    <bean id="hbTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="dbFileHelper" class="com.kc.models.DbFileHelper">
        <property name="hbTemplate" ref="hbTemplate"></property>
    </bean>

    <bean id="fileHelper" class="com.kc.models.FileHelper">
        <property name="dbFileHelper" ref="dbFileHelper"></property>
    </bean>

</beans>
A: 

Are you creating/destroying the SessionFactory between calls? Could you have the hbm2ddl.auto property set to create-drop?

Actually, can you show the Hibernate settings?

Reference

Pascal Thivent
Hibernate settings? u mean my hbm.xml mapping? i will update my Original post with more info including the Controller that actually calls these methods
jonney
@jonney No, I mean the hibernate.cfg.xml or spring equivalent (if you are using spring to configure hibernate).
Pascal Thivent
OK editing now.
jonney
+1  A: 

Hi, i have fixed the problem

i changed <prop key="hibernate.hbm2ddl.auto">create</prop>

to <prop key="hibernate.hbm2ddl.auto">update</prop> and it worked

jonney