File: AccAcc.java
import java.io.*;
public class AccAcc implements Serializable {
private String id;
private int accountnum;
private String acctype;
private String name;
public AccAcc() {
}
public AccAcc(int i, String t, String n) {
accountnum = i;
acctype = t;
name = n;
}
public void setId(String s) {
id = s;
}
public String getId() {
return id;
}
public void setAccountnum(int i) {
accountnum = i;
}
public int getAccountnum() {
return accountnum;
}
public void setAcctype(String s) {
acctype = s;
}
public String getAcctype() {
return acctype;
}
public void setname(String s) {
name = s;
}
public String getName() {
return name;
}
public boolean equals(Object obj) {
if (obj == null) return false;
if (!this.getClass().equals(obj.getClass())) return false;
AccAcc obj2 = (AccAcc)obj;
if (this.id.equals(obj2.getId()) &&
this.accountnum == obj2.getAccountnum() &&
this.acctype.equals(obj2.getAcctype()) &&
this.name.equals(obj2.getName())) {
return true;
}
return false;
}
public int hashCode() {
int tmp = 0;
// Method 1-Concatenate the strings
tmp = (id + accountnum + name + acctype).hashCode();
return tmp;
}
}
File: AccAccTest.java
import java.io.*;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class AccAccTest {
public static void main(String [] args) {
try {
Session session = HibernateUtil.currentSession();
AccAcc acc = new AccAcc(1, "Personal", "John");
session.save(acc);
session.flush();
AccAcc acc2 = new AccAcc();
acc2.setId(1+"Personal"+"John");
session.load(acc2, acc2.getId());
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class HibernateUtil {
public static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
File: AccAcc.hbm.xml
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
table="accacc">
class="string">
File: hibernate.cfg.xml
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1/test
oost
oost
1
org.hibernate.dialect.HSQLDialect
true