私のエンティティ:記事、カテゴリ。これはn:mの関係です。特定のカテゴリーで参照されている記事の数を取得したいのですが。
これが私のエンティティです:
@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 );
}
}
これで、ArticleとCategoryのリポジトリとサービスレイヤーができました。構造を示すためにArticleレイヤーのみを提供します。
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 );
}
}
カテゴリに基づいて記事のサイズを取得するにはどうすればよいですか?ArticleCategory用に別のリポジトリ/サービスレイヤーペアを作成する必要がありますか?それから私が得ているので、それはどのように見えるべきですか:
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: {}