89764

JoinTable doesn't have a regular Long id, but rather EmbeddedId instead, how do I create a Spri

Question:

I've implemented @ManyToMany mapping with an EmbeddedId class, as per the example provided by @Vlad Mihalcea. It works, however I need to create and use Spring's REST repository for this JoinTable Entity.

Usually, I would do something like this to enable the repository:

@CrossOrigin @RepositoryRestResource(collectionResourceRel = "companyproducts", path = "companyproducts") public interface CompanyProductRepository extends PagingAndSortingRepository<CompanyProduct, CompanyProduct.CompanyProductId> { }

But, since in this Entity (JoinTable) I don't have a classic id, like this:

public class FooBar { .... @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; ... }

but instead I have:

public class FooBar { .... @EmbeddedId private FooBarId id; ... }

I cannot use Long or let's say int because the identifier is not of any of those types, it's basically of type FooBarId.

By the way, I will also provide most of that class:

@Embeddable public class FooBarId implements Serializable { @Column(name = "foo_id") private Long fooId; @Column(name = "bar_id") private Long barId; private FooBarId() {} public FooBarId(Long fooId, Long barId) { this.fooId= fooId; this.barId= barId; } ... }

I did try, of course, putting FooBarId instead of Long in the repository declaration, but then in my REST json response I get something like this:

http://localhost:8080/api/foobar/com.example.springrest.entities.foobarid@g43

(note the path to the class itself, not a numeral parameter, eg. 5).

My question is: Is it possible to use repositories with an EmbeddedId instead of Long, int, etc.?

Answer1:

Yes of course

public interface FooBarRepository extends PagingAndSortingRepository<FooBar, FooBarId > { }

the id is the embeddable class so use that instead of the classic primitive.

the problem in the response is probably something related to your save/retrieve logic

<strong>Edit</strong>: based on the response being the class instead of the numeral i expect you retrieved the id from the repository, but you should obviously get the numerical value from the id since now that id is the embeddable class

Recommend

  • C++ design pattern: multiple ways to load file
  • POST XML data with Groovy HTTPBuilder
  • Eclipse code fomatting
  • Windows Forms switch between Panels
  • Clear line in the Python IDLE shell
  • Decoding PubNub messages with golang JSON
  • Objective-C – access extern const with a string containing its name? [duplicate]
  • Streamed transfer restrictions with WCF
  • What's the alternative to IValueFormatter in AutoMapper?
  • Guice, Injecting TypeLiteral when using @AssistedInject
  • How to clear out the contents of a map when clear() method call throws UnsupportedOperationException
  • what is browser's native support according to selenium webdriver
  • send data back from jsp iterator to struts action class
  • JOOQ nested condition
  • How to set the navigation bar to the top in Table View?
  • How can I create a plugin mechanism that calls functions only when the plugin is available?
  • How to accept a collection of a base type in WCF
  • Does “internal” visibility modifier in Kotlin work yet?
  • How to get Fully qualified domain name in unix
  • How to make stdcall from Go
  • Mercurial: Identify file name after rename
  • Why are “sc.addFile” and “spark-submit --files” not distributing a local file to all workers?
  • Comparing user's facebook/twitter friends to site's users in Python/Django
  • Why can't UI components be accessed from a backgroundworker?
  • Add the parent product name to each cart item names in WooCommerce
  • Jquery Knockout: ko.computed() vs classic function?
  • Firefox Extension - Monitor refresh and change of tab
  • C# - Serializing and deserializing static member
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • jquery mobile loadPage not working
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?