2013-06-26 19 views
0

我在Win 7平臺上使用Eclipse Helios。 我有這樣的DAO類Ormlite DatabaseConfigUtil.java生成原始空文件和數據庫字段不會被添加

package com.example.hello; 

import com.j256.ormlite.field.DatabaseField; 
import com.j256.ormlite.table.DatabaseTable; 

@DatabaseTable(tableName = "accounts") 
public class Account { 

     // for QueryBuilder to be able to find the fields 
     public static final String NAME_FIELD_NAME = "name"; 
     public static final String PASSWORD_FIELD_NAME = "passwd"; 

     @DatabaseField(generatedId = true) 
     private int id; 

     @DatabaseField(columnName = NAME_FIELD_NAME, canBeNull = false) 
     private String name; 

     @DatabaseField(columnName = PASSWORD_FIELD_NAME) 
     private String password; 

     Account() { 
       // all persisted classes must define a no-arg constructor with at least package visibility 
     } 

     public Account(String name) { 
       this.name = name; 
     } 

     public Account(String name, String password) { 
       this.name = name; 
       this.password = password; 
     } 

     public int getId() { 
       return id; 
     } 

     public String getName() { 
       return name; 
     } 

     public void setName(String name) { 
       this.name = name; 
     } 

     public String getPassword() { 
       return password; 
     } 

     public void setPassword(String password) { 
       this.password = password; 
     } 

     @Override 
     public int hashCode() { 
       return name.hashCode(); 
     } 

     @Override 
     public boolean equals(Object other) { 
       if (other == null || other.getClass() != getClass()) { 
         return false; 
       } 
       return name.equals(((Account) other).name); 
     } 
} 

和我DatabaseConfigUtil如下,

package com.example.hello; 

import java.io.IOException; 
import java.sql.SQLException; 

import com.j256.ormlite.android.apptools.OrmLiteConfigUtil; 

/** 
* Database helper class used to manage the creation and upgrading of your database. This class also usually provides 
* the DAOs used by the other classes. 
*/ 
public class DatabaseConfigUtil extends OrmLiteConfigUtil { 

    public static void main(String[] args) throws SQLException, IOException { 
     writeConfigFile("ormlite_config.txt"); 
    } 
} 

我的問題是,如果我嘗試生成的原始數據庫配置文件是在res /原始文件夾中倒是沒什麼成功生成在文件中除了

# 
# generated on 2013/06/26 05:18:40 
# 

爲什麼我的數據庫字段不是自動生成的?

回答

3

根據用於writeConfigFile(String fileName)的文檔:

查找類中的註解在當前目錄或下方,並且 在原始文件夾中寫入配置文件到文件名。

假設你的類和DatabaseConfigUtil在同一目錄/包中,它應該工作。正如你所提到的,事實並非如此。

我不知道爲什麼你的res/raw/ormlite_config.txt是空的,但我有一個建議 修復 另一個解決方案。我這樣做(版本4.45):

public class DatabaseConfigUtil extends OrmLiteConfigUtil { 

    // The logger. We cannot use Android's Log class since this is a standalone command line app. 
    private static final Logger logger = Logger.getLogger(DatabaseConfigUtil.class.getName()); 

    /** 
    * The name of the generated ORMLite config file. 
    */ 
    public static final String CONFIG_FILE_NAME = "ormlite_config.txt"; 

    /** 
    * An array of Class-es which will be stored in the local DB. 
    */ 
    public static final Class<?>[] CLASSES = new Class[]{ 
      Alarm.class, 
      Helper.class 
    }; 

    /** 
    * A main method that needs to be executed when a new model class is 
    * introduced to the code base. 
    * 
    * @param args command line parameters (which are ignored). 
    * 
    * @throws IOException when the config file cannot be written to `res/raw/`. 
    * @throws SQLException when one of the Class-es in `CLASSES` contains invalid 
    *      SQL annotations. 
    */ 
    public static void main(String[] args) throws IOException, SQLException { 

     File rawFolder = new File("res/raw"); 

     // Check is `res/raw` exists ... 
     if(!rawFolder.exists()) { 

      // ... if not create it. 
      boolean rawCreated = rawFolder.mkdirs(); 

      if(!rawCreated) { 
       logger.warning("could not create a 'raw' folder inside 'res/'" + 
         " from DatabaseConfigUtil: no DB-config file created!"); 
       System.exit(1); 
      } 
      else { 
       logger.info("created folder `res/raw/`"); 
      } 
     } 

     writeConfigFile(CONFIG_FILE_NAME, CLASSES); 
    } 
} 
相關問題