User-defined ID Generator

zhaozj2021-02-16  62

PO

Package com.dsii.hibernate.po;

Public class company {

Private string id;

PRIVATE STRING NAME;

PRIVATE STRING YEAR;

Public company () {}

Public string getId () {return id;}

Public void setid (string id) {this.id = id;}

Public string getname () {return name;}

Public void setname (String name) {this.name = name;}

Public string getYear () {returnies;

Public void setYear (string year) {this.year = year;

}

Xml

UID_TABLE

Next_hi_value_column

C

Customized ID Generator

Package com.dsii.hibernate.po;

Import java.io.serializable;

Import java.sql.sqlexception;

Import java.util.properties;

Import net.sf.hibernate.hibernateException;

Import net.sf.hibernate.diaalect.diand;

Import net.sf.hibernate.Engine.SessionInTementor;

Import net.sf.hibelnate.type.type;

Import net.sf.hibernate.util.propertieshelper;

Import org.apache.commons.logging.log;

Import org.apache.commons.logging.logfactory;

Import net.sf.hibernate.id.tableGenerator;

Public class customizedidgenerator extends TableGenerator {

PRIVATE STRING Prefix;

Private static final log log = logfactory.getlog (CustomizedIdgenerator.class);

Public Void Configure (Type Type, Properties Params, Dialect D) {

Super.configure (Type, Params, D);

Prefix = params.getProperty ("prefix") == NULL? "pre": params.getproperty ("prefix");

}

Public synchronized serializable generate (sessionimplementor session, object obj) throws sqlexception, hibernateException {

Integer integer = (integer) super.generate (session, obj);

Return prefix integer;}

}

Bean

Package com.dsii.hibernate.po;

Import net.sf.hibernate. *;

Import Net.sf.hibiBernate.cfg.configuration;

Import net.sf.hibernate.tool.hbm2ddl.schemaExport; public class testcompany {

Private static sessionFactory _SESSIONS = NULL;

STATIC {

Try {

Configuration CFG = New Configuration ();

cfg.addclass (company.class);

SchemaExport dbexport = new schemaexport (CFG);

Dbexport.create (true, true);

_SESSIONS = cfg.buildsessionFactory ();

} catCH (mappingexception e) {

E.PrintStackTrace ();

} catch (hibernateException e) {

E.PrintStackTrace ();

}

}

Public static void main (string [] args) throws hibernateException {

System.out.println ("-------------- begin");

Company c1 = new company ();

C1.setname ("DSII");

C1.SETYEAR ("2004");

Company C2 = New Company ();

C2.setname ("DSII");

C2.SETYEAR ("2004");

Session session = _SESSIONS.OPENSESSION ();

Transaction TX = NULL;

Try {

TX = session.begintransaction ();

Session.save (C1);

Session.save (C2);

TX.comMit ();

} catch (hibernateException he) {

IF (tx! = null) {tx.rollback ();} throw he;

Finally {session.close ();

System.out.println ("-------------- End");

}

}

转载请注明原文地址:https://www.9cbs.com/read-15925.html

New Post(0)