JAVA: hibernate + hashmap
Как я должен хранить данные в объекте и вызывать его с помощью хэш-карты ?
означает это : как я должен получить извлечение данных JPA и вызвать его в классе mymethod ?
ListCustomer.put(Long.valueOf(1), new Customer("","","",""));
Что я уже пробовал:
SelectCustomer
private int SelectCustomer() { String Query = "from Customer"; int result = 0; //create session factory sessionFactory = new Configuration().configure("hibcfg.glotng.xml") .addAnnotatedClass(Customer.class).buildSessionFactory(); //create session session = sessionFactory.getCurrentSession(); try { //start transaction session.beginTransaction(); //query CUSTOMER LIST theCustomer = session.createQuery(Query).list(); System.out.println("############ Customer #######################"); for(Iterator iterator = theCustomer.iterator();iterator.hasNext();){ Customer theCustomer = (Customer) iterator.next(); System.out.println(theCustomer.getName()); System.out.println(theCustomer.getIdNumber()); System.out.println(theCustomer.getBirthDate()); } } catch (Exception ex) { ex.printStackTrace(); } finally { session.close(); sessionFactory.close(); return result; } }
модель
package com.amlaPortal.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "customer") public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long Id; @Column(name = "name") private String Name; @Column(name = "id_number") private String IdNumber; @Column(name = "birth_date") private String BirthDate; @Column(name = "mobile_no") private String MobileNo; public Customer(){ } public Customer(String name, String idNumber, String birthDate, String mobileNo) { Name = name; IdNumber = idNumber; BirthDate = birthDate; MobileNo = mobileNo; } public Long getId() { return Id; } public void setId(Long id) { Id = id; } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getIdNumber() { return IdNumber; } public void setIdNumber(String idNumber) { IdNumber = idNumber; } public String getBirthDate() { return BirthDate; } public void setBirthDate(String birthDate) { BirthDate = birthDate; } public String getMobileNo() { return MobileNo; } public void setMobileNo(String mobileNo) { MobileNo = mobileNo; } @Override public String toString() { return "Customer{" + "Id=" + Id + ", Name='" + Name + '\'' + ", IdNumber='" + IdNumber + '\'' + ", BirthDate='" + BirthDate + '\'' + ", MobileNo='" + MobileNo + '\'' + '}'; } }
мой метод
package com.amlaPortal; import com.amlaPortal.entity.Customer; import com.amlaPortal.entity.TotalCount; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * Created by Asyraf-PC on 3/29/2018. */ class mymethodc { List<Map<Long, Customer>> ListCustomer = new ArrayList<Map<Long,Customer>>(); public void myMap(){ //MAP the total Count ListCustomer.put(Long.valueOf(1), new Customer("","","","")); } }