Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA-RS: @OneToOne relationship problem(JPA-RS: @OneToOne relationship problem: related entity is not created after POST)
icon5.gif  JPA-RS: @OneToOne relationship problem [message #1015901] Mon, 04 March 2013 08:48 Go to next message
Daniel Szalay is currently offline Daniel SzalayFriend
Messages: 15
Registered: February 2013
Junior Member
I am using GlassFish 3.1.2.2 with EclipseLink 2.5.0 nightly build 02/26 (org.eclipse.persistence.jpars_2.5.0.v20130226-e0971b1.jar). I had success with @OneToMany relationships, but I can't get @OneToOne working. Consider the following entities:

@Entity
public class SomeEntity implements Serializable {
	private static final long serialVersionUID = 3578346542105360438L;
	
	@Id
	@GeneratedValue
	private long id;
	@OneToOne(mappedBy="some", orphanRemoval=true, cascade=CascadeType.ALL)
	private TestEntity test;
	
	public SomeEntity() {
	}

}


@Entity
public class TestEntity implements Serializable {
	private static final long serialVersionUID = -6118907654374792523L;
	
	@Id
	@GeneratedValue
	private long id;
	@OneToOne(optional=false)
	private SomeEntity some;
	
	public TestEntity() {
	}

}


Let's POST SomeEntity:

{
   "test": { }
}


Response:

{
	"id":101,
	"_relationships":[
		{
			"_link":{
				"href":"http://localhost:8080/app/persistence/pu/entity/SomeEntity/101/test",
				"rel":"test"
			}
		}
	]
}


Let's GET http://localhost:8080/app/persistence/pu/entity/SomeEntity/101/test: the response is '404 - Not Found', and the specified TestEntity is not created. I have also tried to add SomeEntity and TestEntity manually and then retrieving the TestEntity via the link, but even this way I get a 404.

What am I doing wrong? Is this related to weaving?

Quote:
Weaving is required for several features to work: providing relationships as links, editing relationships, and dealing with lazy x-1 relationships. Either deploy to a Java EE compliant server or statically weave your classes.

Lazy x-1 relationships are only supported when using the JPA RS default mapping strategy, which returns those mappings as links by using read-only mappings for the JSON/XML support and providing the links through a weaved attribute.

[Updated on: Tue, 05 March 2013 10:36]

Report message to a moderator

icon14.gif  Re: JPA-RS: @OneToOne relationship problem [message #1016236 is a reply to message #1015901] Tue, 05 March 2013 15:29 Go to previous messageGo to next message
Daniel Szalay is currently offline Daniel SzalayFriend
Messages: 15
Registered: February 2013
Junior Member
SOLVED

Updated everything to the newest nightly (03/05); also updated the javax.persistence jar. Now I get a different response for the POST and I am able to retrieve the entity:

{
	"id":401,
	"_relationships":[
		{
			"_link":{
				"href":"http://192.168.10.134:8080/timelapse/persistence/timelapse/entity/SomeEntity/401/test",
				"rel":"test"
			}
		}
	],
	"test":{
		"_link":{
			"href":"http://192.168.10.134:8080/timelapse/persistence/timelapse/entity/TestEntity/451",
			"method":"GET",
			"rel":"self"
		}
	}
}
Re: JPA-RS: @OneToOne relationship problem [message #1016400 is a reply to message #1016236] Wed, 06 March 2013 11:09 Go to previous messageGo to next message
Daniel Szalay is currently offline Daniel SzalayFriend
Messages: 15
Registered: February 2013
Junior Member
Additional problem:

The relationship link only works right after POST, meaning that if I redeploy the application, it will not work anymore. The reason seems to be that the foreign key is not persisted to the database, it is always NULL.

[Updated on: Wed, 06 March 2013 12:41]

Report message to a moderator

Re: JPA-RS: @OneToOne relationship problem [message #1017859 is a reply to message #1016400] Tue, 12 March 2013 16:46 Go to previous messageGo to next message
Gul Onural is currently offline Gul OnuralFriend
Messages: 12
Registered: February 2013
Junior Member
Are you dropping and recreating tables during deployment ? Or are you doing any cascade delete elsewhere in your code ?
Re: JPA-RS: @OneToOne relationship problem [message #1018213 is a reply to message #1017859] Wed, 13 March 2013 12:17 Go to previous messageGo to next message
Daniel Szalay is currently offline Daniel SzalayFriend
Messages: 15
Registered: February 2013
Junior Member
I have configured my persistence unit this way:

<property name="eclipselink.ddl-generation" value="create-tables" />

[Updated on: Wed, 13 March 2013 12:17]

Report message to a moderator

Re: JPA-RS: @OneToOne relationship problem [message #1018255 is a reply to message #1018213] Wed, 13 March 2013 14:01 Go to previous messageGo to next message
Gul Onural is currently offline Gul OnuralFriend
Messages: 12
Registered: February 2013
Junior Member
After you redeploy your application and before you make any REST calls, can you check the database to make sure that the relationships are still set the way you expected?
Re: JPA-RS: @OneToOne relationship problem [message #1018288 is a reply to message #1018255] Wed, 13 March 2013 15:05 Go to previous messageGo to next message
Daniel Szalay is currently offline Daniel SzalayFriend
Messages: 15
Registered: February 2013
Junior Member
The relationship is corrupted before and after redeploy too! However the relationship link magically works before redeploy; I guess it is cached in the persistence unit, but not persisted to database.

So to use the terminology from my original example: If I check the contents of the TESTENTITY table, I can see that the related instance has been created, but it's foreign key SOME_ID is NULL.

Tested with:

  • EclipseLink 2.5.0 Nightly 20130312
  • EclipseLink 2.5.0 Nightly 20130313

[Updated on: Wed, 13 March 2013 15:25]

Report message to a moderator

Re: JPA-RS: @OneToOne relationship problem [message #1018386 is a reply to message #1018288] Wed, 13 March 2013 18:42 Go to previous messageGo to next message
Gul Onural is currently offline Gul OnuralFriend
Messages: 12
Registered: February 2013
Junior Member
Something like this should work and you should be able to see TEST_ID correctly populated in the SOMEENTITY table in the database:

@Entity
public class SomeEntity implements Serializable {
    private static final long serialVersionUID = 3578346542105360438L;
    
    @Id
    @GeneratedValue
    private long id;
    @OneToOne
    @JoinColumn(name="TEST_ID")
    private TestEntity test;
}

@Entity
public class TestEntity implements Serializable {
    private static final long serialVersionUID = -6118907654374792523L;
    
    @Id
    @GeneratedValue
    private long id;
    @OneToOne(mappedBy="test")
    private SomeEntity some;
}
Re: JPA-RS: @OneToOne relationship problem [message #1018740 is a reply to message #1018386] Thu, 14 March 2013 11:47 Go to previous messageGo to next message
Daniel Szalay is currently offline Daniel SzalayFriend
Messages: 15
Registered: February 2013
Junior Member
Thanks, it works this way. However my entities are valid, so it should work too right?

EDIT: If I do it this way, than it does not work the other way around, meaning: I GET the related entity, and from there if I try to GET the relationship link of the owner, then I get 404. So for me it seems like bidirectional relationships do not work correctly at the moment.

[Updated on: Thu, 14 March 2013 16:19]

Report message to a moderator

Re: JPA-RS: @OneToOne relationship problem [message #1022222 is a reply to message #1018740] Thu, 21 March 2013 13:54 Go to previous message
Gul Onural is currently offline Gul OnuralFriend
Messages: 12
Registered: February 2013
Junior Member
To get one-to-one bidirectional relationship working (in JPA) with SomeEntity and TestEntity, you will need to modify your entities as I suggested.

[Updated on: Thu, 21 March 2013 14:08]

Report message to a moderator

Previous Topic:BLOB Handling in DBWS generated webservice
Next Topic:StuckThreadMaxTime in weblogic
Goto Forum:
  


Current Time: Fri Apr 19 02:29:48 GMT 2024

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

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

Back to the top