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!
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 categoryorg.hibernate.SQL
, or, alternatively, the propertyhibernate.show_sql
enabled.
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
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.