Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Unique constraints in unidirectional one to one relationship
Unique constraints in unidirectional one to one relationship [message #1327794] Fri, 02 May 2014 10:44
Michał Przybylak is currently offline Michał PrzybylakFriend
Messages: 1
Registered: May 2014
Junior Member
Hello everyone.

I'm learning now about JPA 2.1 with EclipseLink as the JPA Provider, and I have following question about unidirectional @OneToOne mapping.

In JSR-338 there is a section 2.10.3.1 ("Unidirectional OneToOne Relationshps") with following paragraph:

Quote:
Table A conttains a foreign key to table B. The foreign key column name is formed as the concatenation of the following: the name of the realtionship property or field of entity A; "_"' the name of the primary key column in table B. The foreign key column has the same type as primary key of table B and there is a unique key constraint on it.


This basically means for me that I can assign target entity to only one source entity. In my opinion EclipseLink in version 2.5.1 with Derby database 10.10.1.1 does not follow JSR-338 in such scenario.

Here is code example:

Source entity:

@Entity
public class FootballPlayer {

	@Id
	private long id;
	
	@OneToOne
	private Ball ball;
	
	public FootballPlayer() {
	}

	public FootballPlayer(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public void setBall(Ball ball) {
		this.ball = ball;
	}

	public Ball getBall() {
		return ball;
	}
}


Target entity:

@Entity
public class Ball {

	@Id
	private long id;

	public Ball() {
	}
	
	public Ball(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}
}


And scenario:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("eclipselink-pu");
EntityManager em = emf.createEntityManager();

Ball ball = new Ball(1L);
FootballPlayer firstPlayer = new FootballPlayer(2L);
FootballPlayer secondPlayer = new FootballPlayer(3L);

firstPlayer.setBall(ball);
secondPlayer.setBall(ball);

em.getTransaction().begin();
em.persist(ball);
em.persist(firstPlayer);
em.persist(secondPlayer);
em.getTransaction().commit(); // Exception?

em.close();
emf.close();


Persistence unit definition in persistence.xml
<persistence-unit name="eclipselink-pu" transaction-type="RESOURCE_LOCAL">
	<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
		<class>com.mprzybylak.minefields.jpa.relationships.onetoone.uni.Ball</class>
		<class>com.mprzybylak.minefields.jpa.relationships.onetoone.uni.FootballPlayer</class>
		
		<properties>
			<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:eclipselink-test;create=true" />
			<property name="javax.persistence.jdbc.user" value="APP" />
			<property name="javax.persistence.jdbc.password" value="APP" />
	
			<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
			<property name="eclipselink.logging.level.sql" value="FINE"/>
			<property name="eclipselink.logging.parameters" value="true"/>
		</properties>
</persistence-unit>


When I add:
@JoinColumn(unique=true)
to ball field in FootballPlayer class - there is an exception as I expect.

So my question is - is there something wrong with my code, or should I report bug?
Previous Topic:Exception when updating object from outside of transaction
Next Topic:Problem removing directory with database
Goto Forum:
  


Current Time: Thu Jul 30 18:22:38 GMT 2015

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

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