cancel
Showing results for 
Search instead for 
Did you mean: 

Entity Relationships - One Specific Use case - Help Required

sid-desh
Advisor
Advisor
0 Kudos

Hello All,

I am facing difficulty understanding one use case for Entity Relationship.

In database tables following relationship exists:

Table : CUSTOMER

ID - INT - PRIMARY KEY

FIRSTNAME - STRING

LASTNAME - STRING

Table ORDER

ID - INT - PRIMARY KEY

SHIPTO - STRING

OREDERDATE - DATE

CUSTID - INT

The relationship is unidirectional and 1:1 with Customer as the owning side.

In the customer entity will this code work. I am not sure.

@Entity

public class Customer{

@ID

private int id;

private String firstName;

private String lastName;

@OneToOne

@JoinColumn(name="CUSTID")

private Order ord;

}

Also it would be great if you could also explain how this code works by showing some

SQL statements which will be issued.

Thanks and Regards

Sidharth Deshpande

Accepted Solutions (0)

Answers (1)

Answers (1)

0 Kudos

Hi Sidharth,

this kind of mapping is not supported by the JPA specification, but you can use a jointable (as usually for n:m) or change the owning side to Order or make the relation bidirectional. The problem is that according to the specification the owning side is responsible for the updates of the relation which is in your case the custid field of Order but the Orderclass doesn't know about the relation. For more details and some examples please have a look at the jsr220 specification and/or the jpa documentation at help.sap.com.

To see the statements which are send to the DB you can use SqlTrace which is also explained at help.sap.com

regards

-Andreas

sid-desh
Advisor
Advisor
0 Kudos

Hello Andreas,

Thank you for your response. But i have one more doubt. How is 1:N unidirectional relationship maintained. I will try to list down my points:

1. A department has many employees : 1:N

2. Relationship is unidirectional from dept to customer.

3. Customer table has a field department id. Hence relational schema is reverse to the entity relationships.

Now as per the spec as a rule Many side should be the owning side. But here since it is unidirectional dept is the owning side.

I read somewhere that we need to anotate the property in the following manner

class dept{

....

@OneToMany

@JoinColumn(name="DEPID")

private List<Employees> e = new .....

}

I was under the impression that 1:1 is a special case of 1:N.

Is this not correct at all.

Please do let me know.

Regards

Sidharth

adrian_goerler
Active Participant
0 Kudos

Hi Sidharth,

the JPA specification does not cover (yet) a unidirectional 1:n mapping with a foreign key column on the many side. Here a join table is required. In this case, the owning side is the 1-side.

Also, the unidirectonal 1:1 mapping with the foreign key column on the target side is not coverd by JPA 1.0.

We will likely see these both mappings in JPA 2.0.

Please see the paragraphs 2.1.8.5.1 and 2.1.8.3.1 of the JPA 1.0 spec for the supported mapping of the unidirectional 1:n and 1:1 mapping, respectively.

Best regards,

Adrian