Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Shared Primary Key examples?
Shared Primary Key examples? [message #1227228] Fri, 03 January 2014 22:56 Go to next message
Deanna D is currently offline Deanna DFriend
Messages: 3
Registered: January 2014
Junior Member
I'm having a terrible time trying to accomplish a simple two-table model with a shared primary key. Can someone point me to a sample that works that shows ALL the code needed to work properly?

I found example #2 here (http://www.eclipse.org/eclipselink/api/2.0/javax/persistence/OneToOne.html) which seems to be just what I need, but I'm unable to make it work.

I looked at the github repo for eclipselink examples but there are very few OneToOne examples and none (that I saw) with shared primary keys.

thanks!
Re: Shared Primary Key examples? [message #1228127 is a reply to message #1227228] Mon, 06 January 2014 13:28 Go to previous messageGo to next message
Deanna D is currently offline Deanna DFriend
Messages: 3
Registered: January 2014
Junior Member
I'll post my code which works, but the foreign key is backward from the way I intended it to be. I have a student class with an ID and then a badge class which I WANT to have a foreign key back to the student. However in this arrangement the student id is generated and inserted into the BADGE table first so I would have to allow that to be the primary key and mark the student table with the foreign key.

Any way to just switch the insert order somehow? Everything else is perfect!

@Entity
public class Student implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	@Column(name="STUDENT_ID")
	private Long id;
        public Long getId() {
    	    return id;
        }
        public void setId(Long value) {
        	id = value; 
        }
	
        public Student() {
    	    badge = new Badge();
        }
	
	private String someField;
	public String getSomeField() {
		return someField;
	}
	public void setSomeField(String value) {
		someField = value;
	}
	
        /** The Student's badge. */
	@OneToOne(cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH})
	@JoinColumn(name="STUDENT_ID", insertable=false, updatable=false)   
	private Badge badge;
	public Badge getBadge() {
		return badge;
	}		

        PrePersist
        public void initialize() {    	
    	    badge.setStudentId(id);    	
        }	
}


@Entity
public class Badge implements Serializable {
	
	
	@Id
	@Column(name="STUDENT_ID")	    
	private Long studentId;    
	public Long getStudentId() {
		return studentId;
	}	
	public void setStudentId(Long value) {
		studentId = value;
	}

	
	
        @Column(name = "SECURITY_LEVEL")
        private Long mSecurityLevel;
        public Long getSecurityLevel() {
        	return mSecurityLevel;
        }
        public void setSecurityLevel(Long value) {
        	mSecurityLevel = value;
        }    
}
Re: Shared Primary Key examples? [message #1228169 is a reply to message #1228127] Mon, 06 January 2014 15:11 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
The way you have it now is that JPA thinks the relationship goes from Student->Badge. JPA will set the foreign key for you, but since you have it setup that Student's Student_id is the foreign key, and marked the 1:1 as read-only, it isn't really useful for you.

If Badge's "STUDENT_ID" is meant to be a foreign key to student, Badge should have a 1:1 relationship to Student that sets it as the foreign key. This relationship should be marked as the ID (@ID) or that it maps the id (@MapsId) if you still want to have a Long studentId in the Badge entity. Student does not need a relationship to Badge, but if it does, it should be marked as mappedby the other side so that the relationship goes from Badge->Student.

@Entity
public class Badge implements Serializable {
@Id
@OneToOne
@JoinColumn(name="STUDENT_ID"
private Student Student;
..
}

or
@Entity
public class Badge implements Serializable {
@Id
private Long studentId;
@OneToOne
@JoinColumn(name="STUDENT_ID")
@MapsId
private Student Student;
..
}

Either options will ensure that Badge gets its Student_ID field from the value in the referenced Student automatically when it is assigned.
Re: Shared Primary Key examples? [message #1228191 is a reply to message #1228169] Mon, 06 January 2014 16:12 Go to previous message
Deanna D is currently offline Deanna DFriend
Messages: 3
Registered: January 2014
Junior Member
That is exactly what I needed. Thanks Chris!!!
Previous Topic:Toplink 9 to EclipseLink migration issue
Next Topic:[SOLVED] Single-Table Multitenancy and Cross-Tenant Access
Goto Forum:
  


Current Time: Thu Apr 25 16:29:07 GMT 2024

Powered by FUDForum. Page generated in 0.03229 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top