1

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried this now, I am still getting empty map as a result.
Can you try to delete the @MapKey(name = "orderNumber") annotation and convert private Map<String, Order> orders; to private List<Order> orders; and tell us if it works? Because if it is not working either this way something could be wrong with your configuration. P.S Have you provided the correct Getter and Setter for the fields since they are private?
I also see in your code Map<String, Order> map = new HashMap(); map.put(null, order); customer.setOrders(map); and map.put(null, order); is surely wrong. You should change it to map.put(order.orderNumber, order);
Thanks for your inputs. I have made my map to List and observed the same issue and observed that my setter method for orders in my customer entity class was not proper so that was causing this issue.
I'm glad I could help ^^
0

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>();

3 Comments

I updated my code and initialized with HashMap, but still it did not work, I got output for orders as : customer orders : {}
Did you try @MapKeyJoinColumn ???As well in your hashmap you are passing key as null.
Yes, now I tried by adding @MapKeyJoinColumn also, bu still getting empty map for orders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.