views:

59

answers:

2

Hi,

I created a table in MySQL:

'object_label' with columns 'id' and 'name'. I inserted values to this table.

In java I created new class -'ObjectLabel':

import javax.persistence.*;

 @Entity
    @Table(name = "object_label")
    public class ObjectLabel  implements Serializable {

        private static final long serialVersionUID = 3475812350796110403L;
        private String name;

        public Long getId() { return id; }

        @Id 
        @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(precision = 10, unique = true, nullable = false, updatable = false)
    public Long getId() {
        return id;
    }

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

        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }


    }

in hibernate.cfg.xml defined:

<mapping class="com.myCompany.model.ObjectLabel" />

I whant to get the valuse from the table, I definded service :

@SuppressWarnings( "unchecked" )
    @Transactional( readOnly = true, propagation = Propagation.SUPPORTS )
    public Collection<T> findAll() {
        Session session = getSessionFactory().getCurrentSession();

        return 
            session.createCriteria( persistentClass 
                    ).setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY
                            ).list();
    }

I gets empty list.

in the database 'select * from 'object_label'' return the values)

what the problem in my code?

Thanks!

A: 

What is the sql that hibernate generates, you can see it in the console/log if show_sql in config is set to true.

Also, use @Entity(name="...") instead of @Entity @Table(name="...."), you're mixing JPA annotations with Hibernate annotations

nkr1pt
No, `@Table(name="....")` and `@Entity(name="...")` are not the same thing. And the former is not Hibernate specific.
Pascal Thivent
I edited the question, add inserted the import. now you can see that I use EJB annotation.
Rivki
+1  A: 

I don't know what persistentClass is in your code but a Criteria Query to retrieve ObjectLabel instances for all records would look like:

List results = session.createCriteria(ObjectLabel.class).list();

Not sure why you're using Criteria.DISTINCT_ROOT_ENTITY since you're not doing any projection.

To see what is happening exactly, I recommend to activate the logging of SQL statements (either using the hibernate.show_sql property as suggested by nkr1pt or via the org.hibernate.SQL logging category). From the Hibernate documentation:

3.5 Logging

...

When developing applications with Hibernate, you should almost always work with debug enabled for the category org.hibernate.SQL, or, alternatively, the property hibernate.show_sql enabled.

Pascal Thivent
I'm working with debug enabled, but I don't get any information on my mistake.
Rivki
@Rivki You should at least get the SQL query that Hibernate is performing. So what SQL query do you see? How does it differ from the expected result?
Pascal Thivent