Java Tutorial/Hibernate/Primary Key

Материал из Java эксперт
Перейти к: навигация, поиск

Composed ID

File: AccAcc.java



   <source lang="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;
   }

}</source>





Use Int As ID which starts from 0

File: Main.java



   <source lang="java">

import java.util.Date; import org.hibernate.Session; public class Main {

 public static void main(String[] args) throws Exception {
   HibernateUtil hibernateUtil = new HibernateUtil();
   hibernateUtil.executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
   Session session = hibernateUtil.getSession();
   Survey survey = new Survey();
   survey.setName("Survey");
   survey.setPurchaseDate(new Date());
   System.out.println(survey.getId());
   
   session.save(survey);
   session.flush();
   
   System.out.println(survey.getId());
   Survey surveyInSession = (Survey) session.get(Survey.class, survey.getId());
   System.out.println(surveyInSession.getName());
   session.close();
   hibernateUtil.checkData("select * from survey");
 }

}</source>





Use Long AS ID

File: Main.java



   <source lang="java">

import java.util.Date; import org.hibernate.Session; public class Main {

 public static void main(String[] args) throws Exception {
   HibernateUtil hibernateUtil = new HibernateUtil();
   hibernateUtil.executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
   Session session = hibernateUtil.getSession();
   Survey survey = new Survey();
   survey.setName("Survey");
   survey.setPurchaseDate(new Date());
   System.out.println(survey.getId());
   
   session.save(survey);
   session.flush();
   
   System.out.println(survey.getId());
   Survey surveyInSession = (Survey) session.get(Survey.class, survey.getId());
   System.out.println(surveyInSession.getName());
   session.close();
   hibernateUtil.checkData("select * from survey");
 }

}</source>





UUID HEX as Primary Key

File: Main.java



   <source lang="java">

import java.util.Date; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; public class Main {

 public static void main(String[] args) throws Exception {
   HibernateUtil hibernateUtil = new HibernateUtil();
   hibernateUtil
       .executeSQLCommand("create table CD(id varchar,title varchar,artist varchar,purchaseDate date,cost decimal(6,2))");
   Session session = hibernateUtil.getSession();
   CD cd = new CD();
   
   session.save(cd);
   session.flush();
   session.close();
   hibernateUtil.checkData("select * from CD");
 }

}</source>