Issues with the JPA map map

Source: Internet
Author: User

There have been a few previous problems with using JPA to map members in entity, and some annotations are easy to confuse, so keep this in mind.

1. Mapping of Map<string,string>

The key and value of this map are both string types, or other underlying types, and can be mapped using the following method:

@ElementCollection
	@CollectionTable (name= "table_name")
	@MapKeyColumn (name= "Propkey")
	@Column (name= "PropValue")
	private map<string,string> properties = new hashmap<string,string> ();

2. Mapping of Map<string,object>

Key is the underlying type, value is the object

@OneToMany
	@JoinTable (name= "Amplifier_prop", inversejoincolumns= @JoinColumn (name= "PropValue"))
	@ Mapkeycolumn (name= "Propkey")
	private map<string,dataconverter> properties = new Hashmap<string, Dataconverter> ();

3. Mapping of Map<object,object>

Both key and value are objects

@OneToMany
	@JoinTable (name= "Testentity_prop", inversejoincolumns= @JoinColumn (name= "PropValue"))
	@ Mapkeyjoincolumn (name= "amplifier_id")
	private map<amplifier,dataconverter> props = new hashmap< Amplifier, dataconverter> ();

4. Mapping of Map<object,string>

Key is an object, value is a string or other underlying type

@ElementCollection
	@CollectionTable (name= "table_name")
	@MapKeyJoinColumn (name= "Propkey")
	@Column ( Name= "PropValue")
	private map<amplifier,string> properties = new hashmap<amplifier,string> ();

When a key is an underlying type or a non-basic type, note @mapkeycolumn and @mapkeyjoincolumn are used exclusively for the underlying type, such as Integer or string, and the latter is for object

When value is an object, use the @onetomany form of the annotation method


The above 4 kinds of ways I have tested, no problem. There should be other ways, lazy to go to Google's just as I write it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.