Last active
January 2, 2016 11:48
-
-
Save wisaruthk/8298638 to your computer and use it in GitHub Desktop.
Openjpa customize @SequenceGenerator for @id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package my.database; | |
import java.sql.Timestamp; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.SequenceGenerator; | |
import javax.persistence.Table; | |
import javax.persistence.Temporal; | |
import javax.persistence.TemporalType; | |
@Entity | |
@Table(name="MY_FILELOG") | |
@SequenceGenerator(name="MYSQ",sequenceName="my.database.MyLogSeq(Sequence=DISTRIBUTE_SEQ)") | |
public class MyFilelog { | |
public static char SUCCESS = 'S'; | |
public static char FAIL = 'F'; | |
@Id | |
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="MYSQ") | |
@Column(name="LOG_ID") | |
private Long pk; | |
@Column(name="PACKAGE_ID") | |
private String packageId; | |
@Column(name="STORE_ID") | |
private String storeId; | |
@Column(name="TERMINAL_ID") | |
private int terminalId; | |
@Column(name="FILE_NAME") | |
private String fileName; | |
@Column(name="FILE_STATUS") | |
private char fileStatus; | |
@Column(name="FILE_TIMESTAMP") | |
@Temporal(TemporalType.TIMESTAMP) | |
private Timestamp fileTimestamp; | |
@Column(name="DESC") | |
private String desc; | |
//getter and setter | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package my.database; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import org.apache.openjpa.jdbc.conf.JDBCConfiguration; | |
import org.apache.openjpa.jdbc.kernel.AbstractJDBCSeq; | |
import org.apache.openjpa.jdbc.kernel.JDBCStore; | |
import org.apache.openjpa.jdbc.meta.ClassMapping; | |
import org.apache.openjpa.jdbc.sql.DBDictionary; | |
import org.apache.openjpa.lib.conf.Configurable; | |
import org.apache.openjpa.lib.conf.Configuration; | |
import org.apache.openjpa.lib.util.Localizer; | |
import org.apache.openjpa.util.UserException; | |
public class MyLogSeq extends AbstractJDBCSeq implements Configurable { | |
private JDBCConfiguration _conf = null; | |
private static Localizer _loc = Localizer.forPackage(MyLogSeq.class); | |
private String _select = null; | |
private String _format = null; | |
private String _seqName = "OPENJPA_SEQUENCE"; | |
private String _schema = ""; | |
public String getSequence(){ | |
return this._seqName; | |
} | |
// set sequence | |
public void setSequence(String seqName){ | |
this._seqName= seqName; | |
} | |
@Override | |
public JDBCConfiguration getConfiguration() { | |
return _conf; | |
} | |
@Override | |
protected Object nextInternal(JDBCStore store, ClassMapping mapping) | |
throws Exception { | |
System.out.println("Hello nextInternal"); | |
Connection conn = getConnection(store); | |
try { | |
/* Customize ID here. | |
* example | |
* return "ABC"+getSequence(conn); | |
*/ | |
return getSequence(conn); | |
} finally { | |
closeConnection(conn); | |
} | |
} | |
/** | |
* Return the next sequence value. | |
*/ | |
private long getSequence(Connection conn) | |
throws SQLException { | |
DBDictionary dict = _conf.getDBDictionaryInstance(); | |
PreparedStatement stmnt = null; | |
ResultSet rs = null; | |
try { | |
stmnt = conn.prepareStatement(_select); | |
dict.setTimeouts(stmnt, _conf, false); | |
synchronized(this) { | |
rs = stmnt.executeQuery(); | |
} | |
if (rs.next()) | |
return rs.getLong(1); | |
// no row !? | |
throw new UserException(_loc.get("invalid-seq-sql", _select)); | |
} finally { | |
// clean up our resources | |
if (rs != null) | |
try { rs.close(); } catch (SQLException se) {} | |
if (stmnt != null) | |
try { stmnt.close(); } catch (SQLException se) {} | |
} | |
} | |
@Override | |
public void endConfiguration() { | |
DBDictionary dict = _conf.getDBDictionaryInstance(); | |
if(_format==null){ | |
_format = dict.nextSequenceQuery; | |
} | |
_schema = _conf.getSchema(); //get schema from persistence.xml | |
this._select = "SELECT nextval for "+_schema+"."+_seqName+" FROM SYSIBM.SYSDUMMY1"; | |
} | |
@Override | |
public void setConfiguration(Configuration arg0) { | |
_conf = (JDBCConfiguration)arg0; | |
} | |
@Override | |
public void startConfiguration() { | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment