Java – is there a better way to keep the values of the map a collection?

I see many maps that can be saved using hibernate, such as map < string, set & entity > > a little lucky (especially because I want everything on one table)

Mapping multimaps with hibernate seems to get the most references. It describes in detail how to implement it using usercollectiontype

I want to know, since it was written four years ago, is there a better way now?

So, for example, I want to make a map of EntityA, set / list < entityb > >

There will be two tables: EntityA and entityb (entityb has a foreign key that returns EntityA)

I don't want any intermediate watches

Solution

My approach to the current project is to use xStream to convert beans / collections to XML:

public static String toXML(Object instance) {
    XStream xs = new XStream();
    StringWriter writer = new StringWriter();
    xs.marshal(instance,new CompactWriter(writer));
    return writer.toString();
}

Then use the lob type in hibernation to persist:

@Lob
@Column(nullable = false)
private String data;

I find this method very general, and you can effectively realize flexible key / value storage through it If you don't like XML format, the xStream framework has a built-in driver to convert objects to JSON Give it a try. It's really cool

Cheers!

Editor: respond to comments Yes, if you want to overcome the limitations of classical methods, you may sacrifice things like indexing and / or search You can implement index / search / external / sub relationships through your own collection / common entity beans – just maintain a separate key / value table, which requires the attribute name / attribute value you want to search

I've seen the number of database designs that require flexible and dynamic products (i.e. creating new properties for domain objects without downtime) patterns. Many database designs use key / value tables to store domain properties and references of owner objects to children These products cost millions of dollars (banking / Telecommunications), so I guess this design has proved effective

Sorry, this is not the answer to your original question because you asked for a solution without an intermediate table

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>