I designed the following database tables:
- "article" with "materialID" as primary key.
- "supplier" with "supplierID" as primary key.
- "procurement" with "materialID" and "supplierID" as composite primary key (including foreign key relationships to tables 1 and 2).
Now I want to deal with them in Spring using JPA. First, I will demonstrate the approach working for the entities "article" and "supplier".
Entity class:
@Entity
public class Article {
@Id
private String materialID;
@Column(nullable = false)
private String shortText; }
JpaRepository for the entity:
@Repository
public interface IArticleRepository extends JpaRepository<Article, String>
{
List<Article> findByShortTextLike(String shortText); //just another search method
}
Service providing transactions to the 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, this system does not seem to work properly for the "procurement" entity. I have already read about the composite primary key challenge and wrote this entity class.
@Entity
@Table
public class Procurement implements Serializable {
private static final long serialVersionUID = 4976268749443066505L;
@EmbeddedId
private ProcId pk;
The class of the embedded id looks like this.
@Embeddable
public class ProcId implements Serializable {
private String materialID;
private int supplierID;
}
Are the entity and the id classes correct? Has anyone an idea how to change the repository / service in order to work with the composite primary key?