0

Spring 3.1 / Hibernate / Jackson を使用して Restfull API サーバーを作成しています。

私は '購入' コントローラー/モデル/Dao を持っています。

ここで、購入を「タグ付け」する機能を追加する必要があります。

したがって、「購入」にリンクされた「tagId」と「tagName」を持つ単純なクラスです。
「購入」は複数の「タグ」を持つことができ、「タグ」は 1 つの「購入」にのみ属することができます。

追加しなければならないこの新しい「タグ」クラスを表す最良の方法は何ですか? いえ

  • 'Tag' モデルに purchaseId 属性を追加して、何らかの方法で注釈を付ける必要がありますか?
  • 「タグリスト」属性を「購入」モデルに追加する必要がありますか?
  • 「PurchaseController」のサブクラスとなる「タグ」コントローラーを作成しますか?
  • 等...

基本的に、Spring を使用してこれを設計するためのベスト プラクティスの方法を探しています。

さらに、私が採用できるデザインパターンに関する提案は大歓迎です。
ここでデコレータパターンが適用されるのでしょうか?

もちろん、すべての購入とタグはデータベースに保持する必要があります。

ありがとう

購入コントローラー:

@Controller
public class PurchaseController
{

    @Autowired
    private IPurchaseService purchaseService;

    @RequestMapping(value = "purchase", method = RequestMethod.GET)
    @ResponseBody
    public final List<Purchase> getAll()
    {
        return purchaseService.getAll();
    }

    @RequestMapping(value = "purchase/{id}", method = RequestMethod.GET)
    @ResponseBody
    public final Purchase get(@PathVariable("id") final Long id)
    {
        return RestPreconditions.checkNotNull(purchaseService.getById(id));
    }

    @RequestMapping(value = "purchase/tagged", method = RequestMethod.GET)
    @ResponseBody
    public final List<Purchase> getTagged()
    {
        return RestPreconditions.checkNotNull(purchaseService.getTagged());
    }

    @RequestMapping(value = "purchase/pending", method = RequestMethod.GET)
    @ResponseBody
    public final List<Purchase> getPending()
    {
        return RestPreconditions.checkNotNull(purchaseService.getPending());
    }

    @RequestMapping(value = "purchase", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public void create(@RequestBody final Purchase entity)
    {
        RestPreconditions.checkRequestElementNotNull(entity);
        purchaseService.addPurchase(entity);
    }
}

購入モデル:

@Entity
@XmlRootElement
public class Purchase implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 6603477834338392140L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long pan;

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Long getPan()
    {
        return pan;
    }

    public void setPan(Long pan)
    {
        this.pan = pan;
    }
}
4

2 に答える 2

1

購入モデルは以下のように変更する必要があります。

@Entity
public class Purchase implements Serializable
{
/**
 * 
 */
    private static final long serialVersionUID = 6603477834338392140L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long pan;

    @OneToMany(mappedBy = "instance", fetch = FetchType.LAZY)
    private Set<Tag> tags;

    public Long getId()
    {
      return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Long getPan()
    {
        return pan;
    }

    public void setPan(Long pan)
    {
        this.pan = pan;
    }

    public void setTags(Set<Tag> tags){
        this.tags = tags;
    }

    @JsonIgnore
    public Set<Tag> getTags(){
        if(tags == null){
           tags = new LinkedHashSet<Tag>();
        }
        return tags;
    }
}

タグモデルは以下のようになります。

@Entity
public class Tag implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({ @JoinColumn(name = "PURCHASE__ID", referencedColumnName = "ID") })
    private Purchase purchase;

    //Other attributes, getters and setters

   @JsonIgnore
   public Purchase getPurchase(){
       return this.purchase;
   }


}

Purchase がリレーションシップ (親) を所有している場合は、PurchaseService と PurchaseController にタグ固有のメソッドを実装するか、Tag に個別のサービス クラスとコントローラ クラスを実装できます。

于 2012-05-30T14:23:04.190 に答える
0

Purchase コントローラーには「タグ付け」の概念が既に含まれています。getTagged

購入モデルにプロパティを含む設計を構築しList<ITag> Tagsます。これが、現在の設計に実装する最も簡単で簡単な方法である可能性があるからです。

于 2012-05-29T21:53:55.520 に答える