顯示具有 hibernate 標籤的文章。 顯示所有文章
顯示具有 hibernate 標籤的文章。 顯示所有文章

2009年3月13日 星期五

用 Spring 取得 Oracle Sequence 及 Hibernate 的設定

Oracle:
create sequence ORDER_SEQ
start with 1 
increment by 1 
nomaxvalue;

Spring:
使用Spring提供的org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer:
OracleSequenceMaxValueIncrementer incr = new OracleSequenceMaxValueIncrementer(dataSource, "ORDER_SEQ");
int seq = incr.nextIntValue();

Hibernate:
<id name="id" column="ID" type="java.lang.Long">
    <generator class="sequence">
        <param name="sequence">ORDER_SEQ</param>
    </generator>
</id>

2008年7月6日 星期日

根據hibernate configuration file建立table schema

程式範例如下:

package mytest;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class SchemaExportTester {

public static void main(String[] args) {
new SchemaExportTester().createSchema();
}

private void createSchema() {
Configuration cfg = new Configuration().configure();
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.setOutputFile("schema.sql");
schemaExport.create(true, true);
System.out.print("ok");
}
}


其中
schemaExport.create(true, true);
第一個參數是表示是否要產生schema.sql,第二個參數是表示是否要在database立建立table。