2015-04-02 105 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(); 
}