Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How should i store the data into the object and calling it by using the hashmap ?

means is : how should i get the data JPA extraction and call it at mymethod class ?

ListCustomer.put(Long.valueOf(1), new Customer("","","",""));


What I have tried:

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;
    }

}



model
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 + '\'' +
				'}';
	}
}


my method
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("","","",""));

    }

}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900