简体   繁体   中英

Creating a JPA Entity from a Map<String,String> representing a database row

UPDATE

While I will not delete this question, in case an answer gets provided that helps someone in the future, the project that this question is related to was abandoned for other reasons.


Question

Is it possible to create a instance of a JPA entity class based on the data in a map of field names to string values representing a database row, in the same way that the entity manager converts a resultset of database column names to values into an entity?

EclipseLink 2.4.1 is being used in this project, and publicly accessible methods specific to the EclipseLink 2.4.1 can be used.

Situation

I am using a web service which will retrieve the state of one or more database rows as they were at time X, by interpreting an audit log. This may not be the right way to go about things, but that is what I have available to work with.

This returns data in the form:

{
    "id_column":{
        "value":"5"
        ...*snip*...
    },
    "enum_column":{
        "value":"VALUE_1"
        ...*snip*...
    },
}

Which is already converted in my code to a map of {"id_column"=>"5" and "enum_column"=>"VALUE_1"}.

This then needs to be converted back into an entity, in this example

*snip*

@Table(schema="stackoverflow", name="exampleEntity")
public class StackoverflowExampleEntity {
    @Id
    @Column(name="id_column")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id_column;

    @Enumerated(EnumType.STRING)
    @Column(name="enum_column")
    private StackOverflowExampleEnum enum_column;

    *snip*
}

This process needs to be generic, and apart from this stumbling block, that should be possible.

Background

I have tried to delve into the EclipseLink source code but was unable to find the part of the code which actually does the conversion between row data and an entity.

I am aware that the following will achieve my goal, but it involves an needless trip to the database, and involves embedding potentially unsafe information into an SQL query (and therefore is a potential SQL injection risk)

Object o = em.createNativeQuery("SELECT '5' AS id_column, 'VALUE_1' AS enum_column", StackoverflowExampleEntity .class).getSingleResult();

I could also create an entity and use the setters, but a.) this is not a generic solution, and b.) requires manual conversion of properties from strings to the property types, something the JPA entity manager already does and knows how to do for managed entities.

I don't think you can be able to find what you want as is... The process in EclipseLink (or any other framework) is more complicated than that.

But you can easily use reflection with java to get what you want. You parse your map and create related object: the key of the map is the field to find and the value is the value to set.

If you need an example, you can look for the (un)marshalling process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM