This question is pretty much asked and answered here:
How to create a composite primary key which contains a @ManyToOne attribute as an @EmbeddedId in JPA?
You need four classes:
- Person.java
- Home.java
- PersonHome.java
- PersonHomePk.java
The Person.java and Home.java files you create with one to many relations to the PersonHome.java. They'll have @Id fields to identify the primary keys. Each will have a @OneToMany relation defined with at least a mappedBy attribute mapping to their respective fields in the PersonHome entity. i.e. in the Person.java you could have something like
@OneToMany(cascade = CascadeType.ALL, mappedBy = "Person")
private Collection<PersonHome> personHome;
The PersonHome.java will have an @EmbeddedId field to identify the PersonHomePk instance declaration which is its primary key (that is, instead of an @Id column you will have an @EmbeddedId annotating a declaration of a class representing the primary key of the join table PersonHome). Any other fields are declared as normal columns. The PersonHome.java will also declare two ManyToOne relations one each to person and home. These will use @JoinColumn annotation (make sure they have the attributes insertable=false and updatable=false). The datatypes will be the Person and Home classes. i.e.
@EmbeddedId
protected PersonHomePk personHomePk;
@Column (name = "type")
private String type;
@JoinColumn(name = "person_id", referencedColumnName = "person_id", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Person person;
You'll need the same for the "Home" declaration too.
Why are you using only a char for "type". I'd recommend a varchar so people who maintain the thing once you're gone will understand the code and database better when you aren't around. 'detached' is easier to understand the 'd'.