Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8027533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:50:34+00:00 2026-06-04T23:50:34+00:00

I’m creating a Restfull API Server using Spring 3.1 / Hibernate / Jackson. I

  • 0

I’m creating a Restfull API Server using Spring 3.1 / Hibernate / Jackson.

I have a ‘Purchase‘ Controller/Model/Dao.

I now need to add the ability to ‘Tag‘ a purchase.

So a simple class with a ‘tagId’ and ‘tagName’ linked back to the ‘Purchase’.
A ‘Purchase’ can have many ‘Tags’, a ‘Tag’ can belong to only one ‘Purchase’.

What is the best way to represent this new ‘Tag’ class I must add? I.e.

  • Should I add purchaseId attribute to ‘Tag’ model and annonate it somehow?
  • Should I add a ‘Tags List’ attribuate to Purchase’ model?
  • Would I create a ‘Tag’ controller that would be a subclass of ‘PurchaseController’?
  • Etc…

Essentially, I’m looking for the best practice way to design this using Spring.

Additionally, any suggestions on design patterns I could employ would be welcome.
Maybe the Decorator pattern is applicable here?

All Purchases and Tags must persist to a database of course.

Thanks

Purchase Controller:

@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);
    }
}

Purchase Model:

@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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:50:36+00:00Added an answer on June 4, 2026 at 11:50 pm

    Purchase model needs to be modified like below.

    @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;
        }
    }
    

    and Tag model will look something like below.

    @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;
       }
    
    
    }
    

    You can implement tag specific methods in PurchaseService and PurchaseController, if Purchase owns the relationship(parent) or implement separate service and controller classes for Tag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.