In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Hibernate case analysis". In daily operation, I believe many people have doubts about Hibernate case analysis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "Hibernate case analysis". Next, please follow the editor to study!
Hibernate (currently in use version 3. 2) provides a variety of ways to generate primary keys. However, so many current generation methods may not meet our requirements. For example, increment can be used when it is convenient for an Hibernate instance, but not when it is clustered. For example, identity, sequence and native are the primary key generation methods provided by the data Bureau, which are often not needed by us, and they also show deficiencies in the program cross-database. And the primary key generated by the algorithm-based generation method is basically a string.
We now need a generation method: using Long as the primary key type, auto-increment, and support for clustering. Then we need to customize one of our primary key generators to implement it.
Hibernate instance code:
Package hibernate
Import java.io.Serializable
Import java.sql.Connection
Import java.sql.PreparedStatement
Import java.sql.ResultSet
Import java.sql.SQLException
Import java.util.Properties
Import org.apache.commons.logging.Log
Import org.apache.commons.logging.LogFactory
Import org.hibernate.HibernateException
Import org.hibernate.MappingException
Import org.hibernate.dialect.Dialect
Import org.hibernate.engine.SessionImplementor
Import org.hibernate.id.Configurable
Import org.hibernate.id.IdentifierGenerator
Import org.hibernate.id.PersistentIdentifierGenerator
Import org.hibernate.type.Type
Public class IncrementGenerator implements IdentifierGenerator, Configurable {
Private static final Log log = LogFactory.getLog (IncrementGenerator.class)
Private Long next
Private String sql
Public Serializable generate (SessionImplementor session, Object object)
Throws HibernateException {
If (sqlpercent null) {
GetNext (session.connection ())
}
Return next
}
Public void configure (Type type, Properties params, Dialect d)
Throws MappingException {
String table = params.getProperty ("table")
If (table==null) table= params.
GetProperty (PersistentIdentifierGenerator.TABLE)
String column = params.getProperty ("column")
If (column==null) column= params.
GetProperty (PersistentIdentifierGenerator.PK)
String schema = params.getProperty
(PersistentIdentifierGenerator.SCHEMA)
Sql = "select max (" + column + ") from" +
(schema==null? Table: schema +'.'+ table)
Log.info (sql)
}
Private void getNext (Connection conn) throws HibernateException {
Try {
PreparedStatement st = conn.prepareStatement (sql)
ResultSet rs = st.executeQuery ()
If (rs.next ()) {
Next = rs.getLong (1) + 1
}
Else {
Next = 1l
}
} catch (SQLException e)
{
Throw new HibernateException (e)
}
Finally {
Try {
Conn.close ()
} catch (SQLException e)
{
Throw new HibernateException (e)
}
}
}
}
Configuration:
Configure id in the corresponding hbm file as follows:
At this point, the study of "Hibernate case Analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.