我使用hibernate
one-to-one
映射来构建Stock
和Category
之间的关系。当我的网络应用程序开始使用以下Category
语句时,会导入sql
表的数据。
insert into Category (name, created_time) values ('top', now()), ('recommend', now()), ('editor choice', now()), ('random', now());
首先,我可以通过执行CategoryDAOImpl.getCategories
获取类别列表,但在执行CategoryDAOImpl.getCategorybyName(String name)
之后,方法CategoryDAOImpl.getCategories
无法正常工作,输出消息显示hibernate sql执行不返回。
我实施one-to-one
映射时是否存在错误?
我该如何解决这个问题?
Stock.java
@Entity
public class Stock {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@OneToOne
private Category category;
@Column(name = "created_time")
private Date date;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
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;
}
/**
* @return the category
*/
public Category getCategory() {
return category;
}
/**
* @param category the category to set
*/
public void setCategory(Category category) {
this.category = category;
}
/**
* @return the date
*/
public Date getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(Date date) {
this.date = date;
}
}
Category.java
@Entity
public class Category {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_time")
private Date date;
@OneToOne(mappedBy = "category")
private Stock stock;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
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;
}
/**
* @return the date
*/
public Date getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(Date date) {
this.date = date;
}
/**
* @return the stock
*/
public Stock getStock() {
return stock;
}
/**
* @param stock the stock to set
*/
public void setStock(Stock stock) {
this.stock = stock;
}
}
CategoryDAOImpl.java
@Repository
@Transactional
public class CategoryDAOImpl implements CategoryDAO {
@Autowired
private SessionFactory sessionFactory;
/*
* (non-Javadoc)
*
* @see com.example.mywebapp.dao.CategoryDAO#getCategories()
*/
@Override
public List<Category> getCategories() {
// TODO Auto-generated method stub
return sessionFactory.getCurrentSession()
.createCriteria(Category.class).list();
}
/*
* (non-Javadoc)
*
* @see
* com.example.mywebapp.dao.CategoryDAO#getCategorybyName(java.lang.String)
*/
@Override
public Category getCategorybyName(String name) {
// TODO Auto-generated method stub
List<Category> categories = sessionFactory.getCurrentSession()
.createCriteria(Category.class)
.add(Restrictions.eq("name", name)).list();
if (categories != null && categories.size() > 0) {
return categories.get(0);
}
return null;
}
/**
* @return the sessionFactory
*/
public SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* @param sessionFactory
* the sessionFactory to set
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
答案 0 :(得分:1)
从hibernate的调试输出中,我找出了导致问题的原因。当来自Category
的查询时,hibernate将使用Stock
加入表,因为它们之间存在one-to-one
映射关系,因此结果不正确。通过在Category
查询上设置投影和相应的结果转换器,它可以正常工作。
@Override
public List<Category> getCategories() {
// TODO Auto-generated method stub
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("id"), "id");
projectionList.add(Projections.property("name"), "name");
projectionList.add(Projections.property("date"), "date");
return sessionFactory.getCurrentSession()
.createCriteria(Category.class).setProjection(projectionList)
.setResultTransformer(Transformers.aliasToBean(Category.class))
.list();
}