Example of using usertype on hibernate3.2

Source: Internet
Author: User
Tags string splitter

Take a closer look at the hibernate Doc and find a lot of surprising things. I regret how useless it is.
The main purpose of this project is.
Database Table: echomessageemail, which has three field IDS (automatic numbers) \ Name \ email (varchar)
Purpose: The e-mail value in the database field is similar to the heweiya@gmail.com; heweiya@21cn.com; www.1718zx.cn. Therefore, it is required to be expressed in list.

That is, the generated datamodel contains a list object.
. HBM. xml

XML Code

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<! --
Mapping File autogenerated by myeclipse-hibernate tools
-->
<Hibernate-mapping>
<Class name = "com. travelsky. hibernate. Po. echomessageemail" table = "echo_message_email">

<ID name = "ID" type = "Java. Lang. Integer">
<Column name = "ID"/>
<Generator class = "native"> </generator>
</ID>
<Property name = "name" type = "Java. Lang. String">
<Column name = "name" length = "50" not-null = "true"/>
</Property>
<Property name = "email" type = "com. travelsky. hibernate. Po. emaillist" lazy = "true">
<Column name = "email"/>
</Property>
</Class>
</Hibernate-mapping>
Corresponding pojo:

Java code

Package com. travelsky. hibernate. Po;
Import java. util. List;

/**
* Echomessageemail generated by myeclipse persistence tools
*/

Public class echomessageemail implements java. Io. serializable {

Private integer ID;
Private string name;
Private list email;

/** Default constructor */
Public echomessageemail (){
}

/** Minimal constructor */
Public echomessageemail (string name ){
This. Name = Name;
}

/** Full constructor */
Public echomessageemail (string name, list email ){
This. Name = Name;
This. Email = Email;
}

// Property accessors

Public integer GETID (){
Return this. ID;
}

Public void setid (integer ID ){
This. ID = ID;
}

Public String getname (){
Return this. Name;
}

Public void setname (string name ){
This. Name = Name;
}

Public list getemail (){
Return email;
}

Public void setemail (list email ){
This. Email = Email;
}

}
One usertype class: emaillist

Java code

Package com. travelsky. hibernate. Po;

Import java. Io. serializable;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. types;
Import java. util. arraylist;
Import java. util. List;

Import org. Apache. commons. Lang. stringutils;
Import org. hibernate. hibernateexception;
Import org. hibernate. hibernate;

/**
* Java. Io. serializable must be referenced, and usertype is deserialization.
* In addition, The Hibernate version I use is 3.2, and the usertype is significantly different from 2.0 to 3.2. Please take a look at the official Doc
*
* @ Author @ Jia Jun. Pay attention to org. hibernate. usertype. usertype. Do not build your own car.
*
*/
Public class emaillist implements java. Io. serializable, org. hibernate. usertype. usertype {

Private list emails;

Private Static final string splitter = ";";

Private Static final int [] types = new int [] {types. varchar };

Public int [] sqltypes (){

Return types;
}

Public class returnedclass (){
// Todo auto-generated method stub
Return list. Class;
}

Public Boolean equals (Object X, object y) throws hibernateexception {
If (x = y)
Return true;
If (X! = NULL & Y! = NULL ){
List xlist = (list) X;
List ylist = (list) y;
If (xlist. Size ()! = Ylist. Size ())
Return false;
For (INT I = 0; I <xlist. Size (); I ++ ){
String str1 = (string) xlist. Get (I );
String str2 = (string) ylist. Get (I );
If (! Str1.equals (str2 ))
Return false;
}
Return true;
}
Return false;
}

Public object nullsafeget (resultset RS, string [] names, object owner)
Throws hibernateexception, sqlexception {
String value = (string) hibernate. String. nullsafeget (RS, Names [0]);
If (value! = NULL ){
Return parse (value );
} Else {
Return NULL;
}
}

Public void nullsafeset (preparedstatement St, object value, int index)
Throws hibernateexception, sqlexception {
System. Out. println ("Set Method excecuted ");
If (value! = NULL ){
String STR = assemble (list) value );
Hibernate. String. nullsafeset (St, STR, index );
} Else {
Hibernate. String. nullsafeset (St, value, index );
}

}

Public object deepcopy (object Value) throws hibernateexception {
List sourcelist = (list) value;
List targetlist = new arraylist ();
Targetlist. addall (sourcelist );
Return targetlist;
}

Public Boolean ismutable (){
Return false;
}

Private string assemble (list emaillist ){
Stringbuffer strbuf = new stringbuffer ();
For (INT I = 0; I <emaillist. Size ()-1; I ++ ){
Strbuf. append (emaillist. Get (I). append (splitter );
}
Strbuf. append (emaillist. Get (emaillist. Size ()-1 ));
Return strbuf. tostring ();
}

Private list parse (string value ){
String [] STRs = stringutils. Split (value, splitter );
List emaillist = new arraylist ();
For (INT I = 0; I <STRs. length; I ++ ){
Emaillist. Add (STRs [I]);
}
Return emaillist;
}

Public object assemble (serializable arg0, object arg1) throws hibernateexception {

// Todo auto-generated method stub
Return NULL;
}

Public serializable disassemble (Object arg0) throws hibernateexception {
// Todo auto-generated method stub
Return NULL;
}

Public int hashcode (Object arg0) throws hibernateexception {
// Todo auto-generated method stub
Return 0;
}

Public object Replace (Object arg0, object arg1, object arg2) throws hibernateexception {

// Todo auto-generated method stub
Return NULL;
}

}
Test class:

Java code

Package com. travelsky. test;
Import java. util. List;
Import java. util. listiterator;
Import com. travelsky. hibernate. Po. echomessageemail;
Import org. hibernate. query;
Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. cfg. configuration;

Public class hibernatetest {

Public static void main (string [] ARGs) throws exception {
Configuration Config = new configuration (). Configure ();
Sessionfactory factory = config. buildsessionfactory ();
Session session = factory. opensession ();
Query query = session. createquery ("from echomessageemail as ");
/**
* Theoretically, lazy loading does not exist. iterator is used for security reasons.
*
*/
Listiterator iterator = query. List (). listiterator ();
Echomessageemail TT = (echomessageemail) iterator. Next (); // only find the first
List emails = TT. getemail ();
For (INT I = 0; I <emails. Size (); I ++ ){
String emailstr = (string) emails. Get (I );
System. Out. println (emailstr );
}
Session. Close ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.