标题说明了一切。现在我的代码将关键值添加到数据库中,我认为问题出在我的模型类或servlet类中。我还想Map<String, String>
值保存到String customerType
场使用jpa将地图值添加到数据库中
型号:
@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;
我把值代入地图,使用servlet类,它看起来像这样:
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!我将地图值发送到Add.jsp页面中的选择标记(简单用户添加表单),从数据保存到数据库的位置。当数据得到保存时,customerType
的格式为customerType.corporate
或customerType.private
,但应该是Corporate
或Private
。
基本上,你建议我摆脱字符串customerType,并保存地图<字符串,字符串>,而不是?所以我不必为地图创建另一张桌子。 –
我不确定你想要完成什么。你想拥有一组客户类型,还是只有一个客户类型?或者你想有一个'customerType'字段,在其中放置地图,格式类似于'customerType.corporate:Corporate,customerType.private:Private'? –
您可以放置地图的字段,格式类似于customerType.corporate:Corporate,customerType.private:Private –