Petr Jeřábek
Petr Jeřábek

Reputation: 91

JPA 2 Custom Entity Fields

I was read this article and I'm looking for Spring – JPA based solution of this problem.

Is possible changing JPA mappings on the fly?

I found this solution, but not working... I'm using Spring / JPA / Hibernate...

Thank you very much!

Upvotes: 1

Views: 2556

Answers (1)

Grigory Katkov
Grigory Katkov

Reputation: 421

There are "dynamic models" in Hibernate, which is similar to "Flex Extensions" you referenced. You can give it a try.

Some time ago I had to do something similar in plain JPA 2 for a PoC project (quick and dirty), and I choose to keep dynamic things as Map, serializing objects myself, looks a little bit ugly but it do the job:

@Access(AccessType.PROPERTY)
@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable(name="dynamic_data", joinColumns=@JoinColumn(name="entity_id"))
private Map<String, String> data_xml;

@Transient
private Map<String, Object> data = new HashMap<>();

protected Map<String, String> getData_xml() { //serialize each value to string 
}

protected void setData_xml(Map<String, String> data_xml) { //convert each value from string
}

Of course it's not ready to be copy-pasted into production code, juts to show the idea.

P.S. I've found some dynamic models demo code in hibernate sources

Upvotes: 2

Related Questions