4

I designed the following database tables:

  1. "article" with "materialID" as primary key.
  2. "supplier" with "supplierID" as primary key.
  3. "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?

PWillms
  • 211
  • 1
  • 3
  • 10
  • No, the PK isn't correctly implemented. Why don't you do the simpler AND right AND more efficient thing: use a single-column, autogenerated primary key for the Procurement entity, like for the two other ones? – JB Nizet Apr 06 '13 at 11:13
  • If I do it that way, will it be still possible to efficiently search for both a materialID and a supplierID in the Procurement table? – PWillms Apr 06 '13 at 11:30
  • Why wouldn't it be? Use a simple JPQL query, and make sure to have an database index on [materialID, supplierID]. – JB Nizet Apr 06 '13 at 11:38

1 Answers1

1

Your ProcId should be defined like this:

@Embeddable
public class ProcId implements Serializable {
    @ManyToOne(optional = false)
    private Article article;
    @ManyToOne(optional = false)
    private Supplier supplier;
}
skirsch
  • 1,640
  • 12
  • 24
  • Finally got my solution above to work, but I will consider your suggestion. I also found the "knowledge database" concerning that topic: http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Advanced – PWillms Apr 06 '13 at 16:26