映射与 DAO 工厂的惰性关系
Mapping lazy relationship with DAO Factory
我想做的是在 Bean 属性和 DAO 之间建立惰性关系。
所以这是我的代码:
豆篇
public class Article {
private Long id;
private Product product;
private Attribut attribut;
private String name;
private Article ParentArticle;
\ getters and setters
}
用于将文章映射到 Bean 的 DAO
private Article map(ResultSet resultSet) throws SQLException {
Article article = new Article();
\set the id of the Article
article.setId(resultSet.getLong("id"));
\get the DAO of each article Bean attribute
ProductDao productDao = daoFactory.getProductDao();
ArticleDao articleDao = daoFactory.getArticleDao();
AttributsDao attributsDao = daoFactory.getAttributDao();`
\set the product of the article by searching the product with his DAO
article.setProduct(productDao.find(resultSet.getLong("idProduct")));
\set the Attribut of the article by searching the attribute with his DAO
article.setAttribut(attributsFonctionsDao.trouver(resultSet.getLong("idAttribut")));
\set the designation of the article article.setDesignation(resultSet.getString("designationArticle"));
\set the Parent Article by searching the article with his DAO
article.setParentArticle(articleDao.trouver(resultSet.getLong("idArticleParent")));
return article;
}
所以我要问的是,是否有一种方法可以映射 Article 对象的属性,因此这里的属性 product、attribut 和 parentArticle 仅具有它们的 id,而不对所有对象收费。我知道 Hibernate 可以提供帮助,但我想在没有 ORM 的情况下手动设置它。
从它的外观来看,ResultSet 实际上是您从数据库中查询的数据,因此来自该结构的任何数据都是持久的,对吗?
您可以做的是使用 session.load(),因为这将始终 return 一个 Hibernate 代理,而无需访问数据库。 Hibernate 代理是一个只有给定标识符的对象,在这种情况下是 id,所有其他属性尚未初始化,它看起来像是来自数据库但实际上不是,它是实际检索的综合表示实体。当对其采取行动时,如果它所引用的数据应该从持久层中消失,代理将抛出 ObjectNotFoundException。
我知道这可能不是您所期望的,因为您已经提到您正在寻找 ORM-free solution
但是我相信这实际上有助于您找到满足您其他期望的解决方案,一种方法not charge all the Object
:)
如果您使用 JPA,Hibernate 的 session.get() 的等效功能由 EntityManager.html#getReference
提供
我想做的是在 Bean 属性和 DAO 之间建立惰性关系。
所以这是我的代码:
豆篇
public class Article {
private Long id;
private Product product;
private Attribut attribut;
private String name;
private Article ParentArticle;
\ getters and setters
}
用于将文章映射到 Bean 的 DAO
private Article map(ResultSet resultSet) throws SQLException {
Article article = new Article();
\set the id of the Article
article.setId(resultSet.getLong("id"));
\get the DAO of each article Bean attribute
ProductDao productDao = daoFactory.getProductDao();
ArticleDao articleDao = daoFactory.getArticleDao();
AttributsDao attributsDao = daoFactory.getAttributDao();`
\set the product of the article by searching the product with his DAO
article.setProduct(productDao.find(resultSet.getLong("idProduct")));
\set the Attribut of the article by searching the attribute with his DAO
article.setAttribut(attributsFonctionsDao.trouver(resultSet.getLong("idAttribut")));
\set the designation of the article article.setDesignation(resultSet.getString("designationArticle"));
\set the Parent Article by searching the article with his DAO
article.setParentArticle(articleDao.trouver(resultSet.getLong("idArticleParent")));
return article;
}
所以我要问的是,是否有一种方法可以映射 Article 对象的属性,因此这里的属性 product、attribut 和 parentArticle 仅具有它们的 id,而不对所有对象收费。我知道 Hibernate 可以提供帮助,但我想在没有 ORM 的情况下手动设置它。
从它的外观来看,ResultSet 实际上是您从数据库中查询的数据,因此来自该结构的任何数据都是持久的,对吗?
您可以做的是使用 session.load(),因为这将始终 return 一个 Hibernate 代理,而无需访问数据库。 Hibernate 代理是一个只有给定标识符的对象,在这种情况下是 id,所有其他属性尚未初始化,它看起来像是来自数据库但实际上不是,它是实际检索的综合表示实体。当对其采取行动时,如果它所引用的数据应该从持久层中消失,代理将抛出 ObjectNotFoundException。
我知道这可能不是您所期望的,因为您已经提到您正在寻找 ORM-free solution
但是我相信这实际上有助于您找到满足您其他期望的解决方案,一种方法not charge all the Object
:)
如果您使用 JPA,Hibernate 的 session.get() 的等效功能由 EntityManager.html#getReference
提供