0

In my DB I nave a table Subject with columns

id (long)
name (String)
ifActive (0 or 1)
text (very long String)

In my web page I must display list of names of Active Subjects. So I declare the following native query in xml file:

<sql-result-set-mapping name="SubjectResult">
    <entity-result entity-class="com.test.Subject">
        <field-result name="id" column="id"/>
        <field-result name="name" column="name"/>
        <field-result name="ifActive" column="ifActive"/>
        <field-result name="text" column="text"/>
    </entity-result>
</sql-result-set-mapping>

<entity class= "com.test.Subject" name= "Subject">
    <named-native-query name="findAllActiveSubject"
                        result-set-mapping="SubjectResult">
        <query>
            select
                *
            from 
                Subject
            where 
                ifActive = 1
        </query>
    </named-native-query>
</entity>

And it works fine - I get list of Subject objects and I can form list of names of Active Subjects. But this is the problem: text in text column is very long and I don't really need to retrive it from DB to form list of names of Active Subjects - I really need only the names of Subjects.

So I change SQL query to following:

<sql-result-set-mapping name="SubjectResult">
    <entity-result entity-class="com.test.Subject">
        <field-result name="id" column="id"/>
        <field-result name="name" column="name"/>
        <field-result name="ifActive" column="ifActive"/>
        <field-result name="text" column="text"/>
    </entity-result>
</sql-result-set-mapping>

<entity class= "com.test.Subject" name= "Subject">
    <named-native-query name="findAllActiveSubject"
                        result-set-mapping="SubjectResult">
        <query>
            select
                name
            from 
                Subject
            where 
                ifActive = 1
        </query>
    </named-native-query>
</entity>

I get exception

org.hibernate.exception.GenericJDBCException: could not execute query

with the root cause

com.microsoft.sqlserver.jdbc.SQLServerException: The column name id is not valid

So I try to change result set to following:

<sql-result-set-mapping name="SubjectResult">
    <entity-result entity-class="com.test.Subject">
        <field-result name="name" column="name"/>
    </entity-result>
</sql-result-set-mapping>

<entity class= "com.test.Subject" name= "Subject">
    <named-native-query name="findAllActiveSubject"
                        result-set-mapping="SubjectResult">
        <query>
            select
                name
            from 
                Subject
            where 
                ifActive = 1
        </query>
    </named-native-query>
</entity>

And I get exception

org.hibernate.exception.GenericJDBCException: could not execute query

with the very strange root cause

com.microsoft.sqlserver.jdbc.SQLServerException: The column name id1_5_0_ is not valid

So..

I can't retrive only several of colums from DB and form Subject only with them? It is ok for me if the rest of properties of Subject would be null - I want to work exactly with Subject and not with list of values or list of arrays of values or something like that.

nik
  • 365
  • 1
  • 4
  • 15

1 Answers1

0

You could use a specific constructor whith HQL

Create the contructor in Subject.java

...
public Subject() {
}

public Subject(String name) {
    this.name = name;
}
...

The query for the select should be

select new Subject(s.name) from Subject s where s.ifActive=1
AndresDLRG
  • 136
  • 7