Java hibernate uses addentity to create sqlsql

I need to apply SQL queries similar to this

SELECT
    id as id,c03 as c03,c34 as c34 
FROM
    (SELECT
        id,c03,c34 
    FROM
        students
    where
        c34 in(
            ?,?,?
        ) 
    order by
        id desc) o 
group by
    c34;

And my java code

private final void retrieveStudents(){
    final List result = currentSession()
            .createsqlQuery("SELECT id as id,c34 as c34 FROM (SELECT id,c34,c03 FROM students where c34 in(:filters) order by id desc) o group by c34;")
            .setParameterList("filters",Arrays.asList(74,1812))
            .list();
    result.forEach(this::consumer1);
}

This query is OK Returns an array of objects that can be iterated, but I want to return a student object, so I add

.addEntity(Student.class)

But I was wrong

Column 'ACTIVE' not found.

I'll try it, too Addscalar, but the same thing happens

.addScalar("id",org.hibernate.type.IntegerType.INSTANCE)
.addScalar("c03",org.hibernate.type.DateType.INSTANCE)

Which is the student's column, but it's not my problem. What should I do? I just think that the alias hibernate should be used to fill the student entity in some way

All I want is a student object filled with ID and C34 values

What is possible that I did wrong?

Solution

For this use case, you don't want hibernate to treat student as an entity, but as a dto

To do this, do not use the addentity method, but setresulttransfomer:

final List result = currentSession()
        .createsqlQuery("SELECT id as id,c34 as c34 " + 
                         "FROM (SELECT id,c03 FROM students " + 
                               "where c34 in(:filters) " + 
                               "order by id desc) o group by c34")
        .setParameterList("filters",1812))
        .addScalar("id",org.hibernate.type.IntegerType.INSTANCE)
        .addScalar("c03",org.hibernate.type.DateType.INSTANCE)
        .addScalar("c34",org.hibernate.type.DateType.INSTANCE)
        .setResultTransformer(Transformers.aliasToBean(Student.class))
        .list();

This is valid for non entity classes as long as the class has a setter that matches the name of the projection column and has a parameterless constructor I've never tested this on an entity class

If you do not have a setter but a constructor for these three fields, you can use:

// choose here the right constructor
java.lang.reflect.Constructor constructor = Student.class.getConstructors()...
// ...
.setResultTransformer(new AliasToBeanConstructorResultTransformer(constructor));

Replace

Edit: I won't use an entity as a dto (but create a specific dto for this use case): if one of your services thinks that the entity is loaded in a normal way (so it is fully initialized), make some modifications (update a field as an example), and save the entity?

In the best case, non nullable fields (on the DB side) will not be initialized. You will receive a constraintviolationexception and trigger a rollback to keep the data safe

In the worst case, you will destroy the data by setting all fields of the entity to null and three loaded fields to null

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
分享
二维码
< <上一篇
下一篇>>