java - JPA Repository / Service design with composite primary keys -
i designed following database tables:
- "article" "materialid" primary key.
- "supplier" "supplierid" primary key.
- "procurement" "materialid" , "supplierid" composite primary key (including foreign key relationships tables 1 , 2).
now want deal them in spring using jpa. first, demonstrate approach working entities "article" , "supplier".
entity class:
@entity public class article { @id private string materialid; @column(nullable = false) private string shorttext; } jparepository entity:
@repository public interface iarticlerepository extends jparepository<article, string> { list<article> findbyshorttextlike(string shorttext); //just search method } service providing transactions user:
@service public class articleservice implements iarticleservice { @autowired private iarticlerepository articlerepository; @override @transactional(readonly = true) public article getarticlebyid(string id) { return this.articlerepository.findone(id); } @override @transactional public article createarticle(string id, string shorttext) { article article = new article(); article.setmaterialid(id); article.setshorttext(shorttext); this.articlerepository.save(article); return article; } unfortunately, system not seem work "procurement" entity. have read composite primary key challenge , wrote entity class.
@entity @table public class procurement implements serializable { private static final long serialversionuid = 4976268749443066505l; @embeddedid private procid pk; the class of embedded id looks this.
@embeddable public class procid implements serializable { private string materialid; private int supplierid; } are entity , id classes correct? has idea how change repository / service in order work composite primary key?
your procid should defined this:
@embeddable public class procid implements serializable { @manytoone(optional = false) private article article; @manytoone(optional = false) private supplier supplier; }
Comments
Post a Comment