0

title says everything. Right now my code adds key values to the database, i think the problem is in my model class or servlet class. Id like to save Map<String, String> value into the String customerType field

Model :

@Id
@SequenceGenerator(name = "my_seq", sequenceName = "seq1", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_seq")
public long id;
private String firstName;
private String customerType;

@ElementCollection
@Column(name="customerType")
public Map<String, String> customerTypes;

I put values into map, using servlet class, which looks like this :

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    showForm(request, response);

}
private void showForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    Map<String, String> map = new LinkedHashMap<String, String>();

       map.put("","");
       map.put("customerType.private", "Private");
       map.put("customerType.corporate", "Corporate");

    Customer customer = new Customer();

    customer.setCustomerTypes(map);
    request.setAttribute("customer", customer);

    request.getRequestDispatcher("/Add.jsp").forward(request, response);    
}

NB! i send map values into select tag in the Add.jsp page (simple user adding form), from where the data gets saved into database. And when the data gets saved the customerType is in the form of customerType.corporate or customerType.private, but should be Corporate or Private.

Juuri Peeter
  • 131
  • 2
  • 20

1 Answers1

1

I think you are using @ElementCollection in a wrong way. If this worked, how would you populate map keys when you read the entity from the database? customerTypes should be in a separate table, take a look at this thread for a solution to a similar problem. Something like this

@ElementCollection
@MapKeyColumn(name="customer_type_key")
@Column(name="customer_type_value")
@CollectionTable(name="customer_types", joinColumns=@JoinColumn(name="customer_id"))
Map<String, String> attributes = new HashMap<String, String>();

UPDATE

Regarding your comment, that you want to have a field in which you would put values from the map in some format. In this case, you don't need customerTypes at all, but you can keep it as @Transient field if you need it for something.

@Transient
Map<String, String> attributes = new HashMap<String, String>();

For most trivial implementation, you can use Map#toString() as value for customerType field.

Servlet:

...
    Map<String, String> map = new LinkedHashMap<String, String>();

    map.put("customerType.private", "Private");
    map.put("customerType.corporate", "Corporate");

    Customer customer = new Customer();
    customer.setCustomerType(map.toString());
...

customerType's value after this will be {customerType.private=Private, customerType.corporate=Corporate}. You will need some custom logic if you want different format.

Community
  • 1
  • 1
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • Basically you suggest me to get rid of the String customerType, and save the Map instead? So i dont have to create another table for the map. – Juuri Peeter Jan 06 '15 at 12:43
  • I'm not sure what you are trying to accomplish. Do you want to have a collection of customer types, or just a single customer type? Or you want to have a `customerType` field in which you would put the map, in some format similar to `customerType.corporate: Corporate, customerType.private: Private`? – Predrag Maric Jan 06 '15 at 12:52
  • field in which you would put the map, in some format similar to customerType.corporate: Corporate, customerType.private: Private – Juuri Peeter Jan 06 '15 at 12:56