2015-04-02 49 views
1

我使用hibernateone-to-one映射建立StockCategory之间的关系。当我的web应用程序开始使用以下sql语句时,会导入Category表的数据。在插入数据后休眠一对一查询错误

insert into Category (name, created_time) values ('top', now()), ('recommend', now()), ('editor choice', now()), ('random', now()); 

起初,我可以通过执行CategoryDAOImpl.getCategories类别列表,但执行CategoryDAOImpl.getCategorybyName(String name)后,该方法CategoryDAOImpl.getCategories将无法​​正常工作,输出消息显示休眠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

与论坛网站不同,我们不使用“谢谢”或“任何帮助赞赏”,或在[so]上签名。请参阅“[应该'嗨','谢谢',标语和致敬从帖子中删除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be - 从帖子中删除)。顺便说一句,它是“提前致谢”,而不是“感谢先进”。 – 2015-05-01 04:36:04

回答

1

星期五om休眠的调试输出,我想出了导致问题的原因。当从Category查询时,休眠将加入表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(); 
}