[英]Hibernate annotations
I created a table in MySQL: 我在MySQL中创建了一个表:
'object_label' with columns 'id' and 'name'. 带有“ id”和“ name”列的“ object_label”。 I inserted values to this table.
我在此表中插入了值。
In java I created new class -'ObjectLabel': 在Java中,我创建了新类-'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: 在hibernate.cfg.xml中定义:
<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) 在数据库中'select * from'object_label'返回值)
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: 我不知道您的代码中有什么
persistentClass
,但是检索所有记录的ObjectLabel
实例的条件查询看起来像:
List results = session.createCriteria(ObjectLabel.class).list();
Not sure why you're using Criteria.DISTINCT_ROOT_ENTITY
since you're not doing any projection. 由于您没有进行任何投影,因此不确定为什么要使用
Criteria.DISTINCT_ROOT_ENTITY
。
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). 要查看实际情况,我建议激活SQL语句的日志记录(使用nkr1pt建议的
hibernate.show_sql
属性或通过org.hibernate.SQL
日志记录类别)。 From the Hibernate documentation: 从Hibernate文档中:
3.5 Logging
3.5记录
...
...
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.使用Hibernate开发应用程序时,几乎应该始终为类别
org.hibernate.SQL
启用debug
,或者启用hibernate.show_sql
属性。
What is the sql that hibernate generates, you can see it in the console/log if show_sql in config is set to true. 休眠生成的sql是什么,如果config中的show_sql设置为true,则可以在控制台/日志中看到它。
Also, use @Entity(name="...") instead of @Entity @Table(name="...."), you're mixing JPA annotations with Hibernate annotations 另外,使用@Entity(name =“ ...”)而不是@Entity @Table(name =“ ....”),将JPA注释与Hibernate注释混合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.