[英]Hibernate: Composite-id Service
My Entities: Article, Category. 我的实体:文章,类别。 It's an n:m relationship.
这是n:m关系。 I would like to get the number of Articles referenced with a specific Category.
我想获取特定类别引用的文章数量。
Here are my entities: 这是我的实体:
@Entity
@Table( name = "tbl_articles" )
public class Article implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column( nullable = false )
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Integer id;
@Column( nullable = false )
private String title;
@Column( nullable = false )
private Date creationDate = new Date();
@Lob
@Type( type = "org.hibernate.type.StringClobType" )
@Column( nullable = false )
private String text;
// Getter + setter
}
@Entity
@Table( name = "tbl_categories" )
public class Category implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column( nullable = false )
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Integer id;
@Column( nullable = false )
private String title;
// Getter + setter
}
@Entity
@Table( name = "tbl_articles_categories" )
@AssociationOverrides({
@AssociationOverride( name = "pk.article", joinColumns = @JoinColumn( name = "article_id" ) ),
@AssociationOverride( name = "pk.category", joinColumns = @JoinColumn( name = "category_id" ) )
})
public class ArticleCategory
{
@EmbeddedId
private ArticleCategoryPK pk = new ArticleCategoryPK();
public ArticleCategoryPK getPk() ...
public void setPk( ArticleCategoryPK pk ) ...
@Transient
public Article getArticle()
{
return pk.getArticle();
}
public void setArticle( Article article )
{
pk.setArticle( article );
}
@Transient
public Category getCategory()
{
return pk.getCategory();
}
public void setCategory( Category category )
{
pk.setCategory( category );
}
}
Now I have the repository and service layers for Article and Category. 现在,我有了文章和类别的存储库和服务层。 I will only provide the Article layers to show the structure.
我将仅提供“文章”层以显示结构。
public abstract class BaseDAO<E>
{
public abstract List<E> list( int offset, int limit );
public abstract void delete( int id );
public abstract void save( E entity );
public abstract E get( int id );
public abstract List<E> list();
public abstract Integer size();
}
@Repository
public class ArticleDAO extends BaseDAO<Article>
{
@Autowired
private SessionFactory sessionFactory;
@Override
public void delete( int id )
{
Article article = get( id );
if ( article != null )
{
sessionFactory.getCurrentSession().delete( article );
}
}
@Override
public void save( Article article )
{
sessionFactory.getCurrentSession().save( article );
}
@Override
public Article get( int id )
{
return ( Article ) sessionFactory.getCurrentSession().load( Article.class, id );
}
@SuppressWarnings( "unchecked" ) // The type is determined by the HQL query but the interface returns an untyped list
@Override
public List<Article> list()
{
return sessionFactory.getCurrentSession().createQuery( "from Article" ).list();
}
@Override
public Integer size()
{
Number ret = ( Number ) criteria.setProjection( Projections.rowCount() ).uniqueResult();
return ret.intValue();
}
}
public abstract class BaseService<E, D extends BaseDAO<E>>
{
protected D dao;
public BaseService()
{
}
protected D getDao()
{
return dao;
}
@Autowired
protected void setDAO( D dao )
{
this.dao = dao;
}
@Transactional
public void delete( int id )
{
dao.delete( id );
}
@Transactional
public void save( E entity )
{
dao.save( entity );
}
@Transactional
public E get( int id )
{
return dao.get( id );
}
@Transactional
public List<E> list()
{
return dao.list();
}
@Transactional
public Integer size()
{
return dao.size();
}
}
@Service
public class ArticleService extends BaseService<Article, ArticleDAO>
{
public ArticleService()
{
setDAO( dao );
}
}
How can I get the size of Articles based on a Category? 如何获得基于类别的文章大小? Do I need to create another Repository/Service layer pair for ArticleCategory?
我是否需要为ArticleCategory创建另一个存储库/服务层对? How should it look like because then I'm getting:
看起来如何,因为然后我得到:
No matching bean of type [com.example.model.ArticleCategoryDAO] found for
dependency: expected at least 1 bean which qualifies as autowire candidate for
this dependency. Dependency annotations: {}
Your exception has nothing to do with your question and Hibernate, end everything to do with Spring. 您的异常与您的问题无关,并且休眠,结束与Spring有关的一切。 Spring doesn't find any Spring bean of type
ArticleCategoryDAO
. Spring找不到任何
ArticleCategoryDAO
类型的Spring bean。 Your probably forgot to annotated it with @Repository
. 您可能忘记了使用
@Repository
对其进行注释。
BTW, you ask if you should create a DAO for ArticleCategory
, but the error message shows that you have created one already. 顺便说一句,您询问是否应该为
ArticleCategory
创建一个DAO,但错误消息表明您已经创建了一个DAO。
Side question: why not simply create a ManyToMany association between Article and Category? 附带问题:为什么不简单地在Article和Category之间创建ManyToMany关联? The
ArticleCategory
entity isn't needed at all. 根本不需要
ArticleCategory
实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.