Learner
Learner

Reputation: 21393

Using Map in Hibernate is not working

I am trying to create a simple program by using java.util.Map. I have created a Customer entity class with a map of Order classes. Here are my Java classes:

Customer.java

@Entity
public class Customer {
    @Id
    @GeneratedValue
    private Integer id;

    @OneToMany(mappedBy = "customer")
    @MapKey(name = "orderNumber")
    private Map<String, Order> orders;
}

Order.java

@Entity
@Table(name="TB_ORDER")
public class Order {
    @Id
    @GeneratedValue
    private Integer id;
    private String orderNumber;
    @ManyToOne
    private Customer customer;
}

Here is my program that tries to save a customer with some orders and then I am trying to display the saved data:

public class AppTest {
    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        saveCustomer(session);
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        showCustomer(session);
        HibernateUtil.getSessionFactory().close();
    }

    private static void showCustomer(Session session) {
        session.getTransaction().begin();
        List<Customer> list = session.createQuery("from Customer").list();
        for (Customer customer : list) {
            System.out.println("customer id : "+customer.getId()+ ", customer orders : "+customer.getOrders());
        }
        session.getTransaction().commit();
    }

    private static void saveCustomer(Session session) {
        session.getTransaction().begin();
        Customer customer = new Customer();
        Order order = new Order();
        order.setCustomer(customer); order.setOrderNumber("100");
        Map<String, Order> map = new HashMap();
        map.put("100", order);
        customer.setOrders(map);
        session.save(customer); session.save(order);
        session.getTransaction().commit();
    }
}

After running the program, Hibernate generated below DDL & DML commands:

Hibernate: create table Customer (id number(10,0) not null, primary key (id))
Hibernate: create table TB_ORDER (id number(10,0) not null, orderNumber varchar2(255 char), customer_id number(10,0), primary key (id))
Hibernate: alter table TB_ORDER add constraint FK_qr7tjivmclv5trf0sbwxki4v foreign key (customer_id) references Customer

Hibernate: insert into Customer (id) values (?)
Hibernate: insert into TB_ORDER (customer_id, orderNumber, id) values (?, ?, ?)
Hibernate: select customer0_.id as id1_0_ from Customer customer0_
customer id : 1, customer orders : null

Hibernate generated only single select query to get the Customer details but not getting any data from TB_ORDER table. So when I try to get the Orders from Customer I am getting null. Please let me know where is the mistake in this code.

Upvotes: 1

Views: 253

Answers (2)

FrancescoM
FrancescoM

Reputation: 1400

It looks like you are missing the @JoinColumn annotation through which you tell who is the owner of the relationship. For this reason i think you should change the class order like this:

@Entity
@Table(name="TB_ORDER")
public class Order {
    @Id
    @GeneratedValue
    private Integer id;
    private String orderNumber;
    @ManyToOne
    @JoinColumn(name = "customer_id")
    private Customer customer;
}

And should work fine.

Upvotes: 2

prashant thakre
prashant thakre

Reputation: 5147

You have to also use @MapKeyJoinColumn as well its better to initialize your Map in pojo and try again .

private Map<String, Order> orders =new Map<String, Order>();

Upvotes: 0

Related Questions