How do I map a root request to a POJO when I don't have an Entity in my project?

I was translating the ETL process from a tool to a Java batch API application. In this process ETL. In the current version (with a tool), we have many SQL statements that concatenate different tables to generate the desired result. Java translation is now available by JPA.

I would like to use my own queries. This would be nice, because it would not have to create entities for every table used in the query, and I could use POJOs for the query result (I also wouldn't need to rewrite queries). Reading this answer I know I can use @SqlResultSetMapping

. The problem is that I don't have any object in my project, so I don't know where to place this annotation. Is there somewhere where I can put this annotation for the entity dispatcher to find it?

PS: In my proof of concept, I am currently manually converting from an array of objects to POJOs, but I really don't like this approach.

Adding an annotation @Entity

to the POJO will prevent my application from starting:

Caused by: org.hibernate.HibernateException: Missing table: MyTable

I'm not sure (looking for it right now), but I think it might be caused by this property in my persistence.xml

<property name="hibernate.hbm2ddl.auto" value="validate"/>

      

+5


source to share


3 answers


Actually I found the answer I was looking for :

I can define behavior @SqlResultSetMapping

using XML in orm.xml, so this definition:

@SqlResultSetMapping(
        name = "BookValueMapping",
        classes = @ConstructorResult(
                targetClass = BookValue.class,
                columns = {
                    @ColumnResult(name = "id", type = Long.class),
                    @ColumnResult(name = "title"),
                    @ColumnResult(name = "version", type = Long.class),
                    @ColumnResult(name = "authorName")}))

      



Will be defined in XML like this:

<sql-result-set-mapping name="BookValueMappingXml">
    <constructor-result target-class="org.thoughts.on.java.jpa.value.BookValue">
        <column name="id" class="java.lang.Long"/>
        <column name="title"/>
        <column name="version" class="java.lang.Long"/>
        <column name="authorName"/>
    </constructor-result>
</sql-result-set-mapping>

      

Lets me define it without the need for an entity.

+8


source


You can map native sql to POJO using JPA. POJO just needs @Entity and @Id. Simple example:

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class TodoQueryModel {

    @Id
    private Long id;

    private String description;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "TodoQueryModel [id=" + id + ", description=" + description
            + "]";
    }
}

      

Some methods in your JPA impl:



private void queryWithNativeSQL() {
    List<TodoQueryModel> todoList = em.createNativeQuery("SELECT id, description FROM [whatever you want as long as it returns 'id' and 'description' as column names]", TodoQueryModel.class)
        .setParameter(1, "foobar");
        .getResultList();

    for (TodoQueryModel todo : todoList) {
        System.out.println(todo);
    }

    System.out.println("Size: " + todoList.size());
}

      

You can also use @Column (name = "barfoo") to map columns to attributes whose names do not match.

The @Id column must uniquely identify the instance in the JPA context.

+4


source


In the past (before JPA), we used iBatis as an ORM tool (now called Mybatis). I'm still a big fan of this because you have a lot of flexibility in writing your SQL. You can actually optimize your queries, especially if you want to decide in what order the join is made. All SQL statements and mappings (columns for POJO and vice versa) are executed in the XML file. The current version can also use annotations, which you would like with JPA.

More information: http://mybatis.github.io/mybatis-3/

+1


source







All Articles