Entity Master has a simple @Id and @OneToMany relationship with Detail
Entity Detail has a cascaded key using @IdClass and a @ManyToOne relationship with Master
Master m = new Master('masterkey');
Detail d = new Detail(m, 0);
em.createQuery("select count(d) from Detail d where d=:d")
.setParameter("d", d)
.getSingleResult()
leads to em.contains(m) == true which results in insert into table at flush/commit time See attached test case