Anne Buit wrote:
Hey trellised,
Haven't encountered this specific situation myself, but I'd consider trying to get the mappedby property to work. The relationship needs an 'owner'. The mappedby property operates off the field/property name, depending on which one you map the annotations.
There is a nice example at
http://download.oracle.com/javaee/5/api/javax/persistence/OneToOne.html, i hope it helps.
Hi Anne,
Thanks for your response!
I now have the mappedBy attribute working (I didn't know I was supposed to use the name of the property on the main class, instead of the name of the main class itself) but alas, I am back to my original problem of the compilation working, execution working - no errors thrown - yet the EXTENSION insertion is totally ignored. It never happens.
I am starting to get really stressed over this.
I changed my classes as follows:
Code:
@Entity
@Table(name = "Main")
public class Main {
@SequenceGenerator(name = "generator", sequenceName="MAIN_SEQ")
@Id
@GeneratedValue(strategy = SEQUENCE, generator = "generator")
@Column(name = "MAIN_ID", unique = true, nullable = false)
private Long mainId;
@OneToOne(optional=false)
@JoinColumn(name="MAIN_ID", unique=true, nullable=false, updatable=false)
private Extension extension;
}
Code:
@Entity
@Table(name = "Extension")
public class Extension {
@Id
@Column(name = "EXTENSION_ID", nullable = false)
private Long extensionId;
@OneToOne(optional=false, mappedBy="extension")
private Main main;
}
I suppose the issue IO have that complicate this scenario is the ID column names between my MAIN and EXTENSION table are not the same. One is MAIN_ID the other is EXTENSION_ID. For MAIN_ID as you can see in annotation above it is setup as a PK with a sequence generator. For EXTENSION, EXTENSION_ID is the PK and is a FK to MAIN_ID (when a MAIN record is created, it may or may not get an EXTENSION record created for it, which would have an EXTENSION_ID set to the value of MAIN_ID.
If I leave my Extension class without any Generator annotation, then Hibernate simply ignores it, never attempts to insert it when I save Main.
If I do provide some Generator annotation (for example I tried IDENTITY earlier) then Hibernate does attempt to insert it, however it did not set the ID as I had hoped so I get an error from Hibernate that I am supposed to assign the value before calling Save().
If the Main entry is brand new, and uses a sequence to generate it's PK, then how can I expect to provide Extension with this ID before I call Save() ???
Edit: Also, since the example did not mention having the id in a separate field with @Id annotated above it, like in my class. I just tried removing it from my class, so all I have is the reference to Extension. This fails however:
Quote:
org.hibernate.AnnotationException:No identifier specified for entity: com.test.Extension
So apparently the example does not even work