2014-11-05 88 views
1

我實現了一個自定義的生成,對我的申請,我想發出一個字符串作爲第二個參數IdentifierGenerator接口,但我沒有得到任何線索如何做到這一點。不幸的是,因爲下面的代碼,它將null2設置爲生成的密鑰。請幫忙。發送字符串第二個參數IdentifierGenerator - 休眠

我想送一個字符串,它是從客戶端作爲第二個參數的「日期」。

謝謝。

public class CourierTransImpl implements IdentifierGenerator{ 
private String appendString; 
@Override 
public Serializable generate(SessionImplementor session, Object arg1) 
     throws HibernateException { 
    Connection connection = session.connection(); 
    int id=0; 
     try { 

      PreparedStatement ps = connection 
        .prepareStatement("SELECT MAX(TRANS_ID) as value from SecurePass.COURIER_TRANSACTIONS_SER_TABLE"); 

      ResultSet rs = ps.executeQuery(); 
      if (rs.next()) { 
       id = rs.getInt("value"); 
       id++; 
      } 
      ps = connection 
        .prepareStatement("INSERT INTO SecurePass.COURIER_TRANSACTIONS_SER_TABLE VALUES("+id+")"); 
      ps.execute(); 
     } catch (SQLException e) {  
      e.printStackTrace(); 
     } 
     return appendString+id; 
} 
public String getAppendString() { 
    return appendString; 
} 
public void setAppendString(String appendString) { 
    this.appendString = appendString; 
} 

} 

回答

0

您可以實現Configurable接口和覆蓋configure爲您的要求。通過這樣做,如果你想通過一些動態值,那麼你可以在你的實體定義的@Transient屬性,然後訪問你只能通過一個靜態值作爲參數傳遞給CourierTransImpl

,在你的CourierTransImpl類屬性。

詳細說明:

例如,讓我們說,有一個實體,稱爲Employee,它有一個名爲empType那麼你可以這樣定義實體的過渡性質。

@Entity 
public class Employee { 

    @Id 
    @GeneratedValue(generator = "UniqueIdGenerator") 
    @GenericGenerator(name = "UniqueIdGenerator", strategy = "com.CourierTransImpl", 
    parameters = { @Parameter(name = "appendString", value = "Emp") }) 
    private String id; 
    private String name; 
    @Transient 
    private String empType; 

    // Getters & Setters 
} 

在上面的代碼中,你可以看到,我們設置的參數appendString,這是我們在這裏設置爲「的Emp」靜態值。

現在CourierTransImpl類實現可配置接口:

public class CourierTransImpl implements IdentifierGenerator, Configurable { 

private String appendString; 

@Override 
public Serializable generate(SessionImplementor session, Object object) 
     throws HibernateException { 
    Connection connection = session.connection(); 
    int id = 0; 
    try { 
     Employee emp = (Employee) object; 
     id = ..; // your logic to get the id from database 

     // Now you can use the parameter appendString which is static value set to "Emp" 
     // You can also access any of the employee properties here, so in your code you can set the required value dynamically. 
     return appendString + emp.getEmpType()+id; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return appendString + id; 
} 

@Override 
public void configure(Type type, Properties params, Dialect d) 
     throws MappingException { 
    setAppendString(params.getProperty("appendString")); // Here we are setting the parameters. 
} 

// Setters & Getters 

}

在這個例子中,如果我創建的Employee一個對象,並設置empType一些價值說「經理人」,那麼休眠生成和ID像「Emp1Manager」。

+0

很不錯的..所以非常感謝我一直在尋找這只是..! – Abhijeet 2014-11-06 05:39:28

0

你的問題還不清楚,但它顯示null2的原因是你的appendString爲空,而不是initialized.I猜你需要的appendString設定的日期。

+0

其實這應該是問題而不是答案評論。 – Chaitanya 2014-11-05 12:14:01

+0

如果你研究它清楚地理解問題的文本。現在,我甚至都不需要'appendString' – Abhijeet 2014-11-06 05:40:29

相關問題