I am using hibernate and in my data model I have I am using Hibernate.Inheritence Single-Table in two different places but there is a problem when they react with eachother. Let me try to simplify with this example.
Basically what happens is when I do letter.setWord(word) then save it the word doesn’t have the reference back to the letter. From my understanding of mappedBy, you don’t have to explicitly set and save it on both ends do you? When I look in the database, the field is properly being set with the guid of the Word it is referencing. Am I doing something wrong?
Side Note: The reason I have the annotations on the getters is because hibernate won’t pick them up at all if they are put directly on the global variables. (I was getting no primary key errors set on the classes extending from Letter). Does hibernate just not handle multiple Inheritence Tables referencing eachother? When I only have one Inheritence table everything works fine with the annotations directly on the global variables.
Thanks,
Mike
@Entity
@Table(name="Letter")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name = "LETTER_TYPE",
discriminatorType=DiscriminatorType.STRING)
public abstract class Letter {
private String id;
private String value;
private Word word;
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDHexGenerator")
@Access(AccessType.PROPERTY)
public String getId() {
return id;
}
@Column(name = "VALUE")
@Access(AccessType.PROPERTY)
public String getValue() {
return value;
}
@ManyToOne
@JoinColumn(name = "WORD_ID")
@Access(AccessType.PROPERTY)
public Word getWord() {
return word;
}
********BASIC SETTERS WITH NO ANNOTATIONS********
}
@Entity
@Table(name="Word")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name = "WORD_TYPE",
discriminatorType=DiscriminatorType.STRING)
public abstract class Word {
private String value;
private List<Letter> letters;
private String id;
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDHexGenerator")
@Access(AccessType.PROPERTY)
public String getId() {
return id;
}
@Column(name = "VALUE")
@Access(AccessType.PROPERTY)
public String getValue() {
return value;
}
@OneToMany(mappedBy="word")
@Cascade({CascadeType.ALL})
@Access(AccessType.PROPERTY)
public List<Letter> getLetters() {
return letters;
}
***************BASIC SETTERS WITH NO ANNOTATIONS**********
}
Your understanding is mistaken, you are responsible for maintaining correct in memory state of the entities that you modify. Hibernate does not automatically go and add the new object to the in-memory entity on the other end.