aspose file tools*
The moose likes Object Relational Mapping and the fly likes Please explain this @OneToOne Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Please explain this @OneToOne " Watch "Please explain this @OneToOne " New topic
Author

Please explain this @OneToOne

Gautam Ry
Ranch Hand

Joined: Dec 30, 2008
Posts: 41
Hi
I am very new to EJB3.0. I have some doubt on the following one to one uni-directional
realtionship.


@Entity (name = "Orders2")
@Table(name = "Orders2")
public class Orders implements Serializable {
@Id
@Basic(optional = false)
@Column(name = "OrderId")
private String orderId;
@Column(name = "OrderName")
private String orderName;
@OneToOne(cascade={CascadeType.PERSIST})
private Shipments objShipment;
}


@Entity(name = "Shipments2")
@Table(name = "Shipments2")

public class Shipments implements Serializable {
@Id
@Basic(optional = false)
@Column(name = "ShpmentId")
private String shpmentId;
@Column(name = "City")
private String city;
}

Based on the above relationship, I have some queries

A) the underlying table have following columns

Orders2 -table
OrderId , OrderName ,OBJSHIPMENT_SHPMENTID
Shipments2 -table
ShpmentId, City

Am I right sir?


B) If I write @PrimaryKeyJoinColumn (name="OrderId",referencedColumnName="ShpmentId") just above
private Shipments objShipment in the Orders class, then changed fields are


Orders2 -table
OrderId , OrderName

Shipments2 -table
ShpmentId, City

we can dropped OBJSHIPMENT_SHPMENTID field in the Orders2 table.
Please, rectify me If I am mistaken.

C) If I write
@JoinColumn(name="Relationship",referencedColumnName="ShpmentId")

just above private Shipments objShipment in the Orders class then changed tables are as
follows:

Orders2 -table
OrderId , OrderName, Relationship

Shipments2 -table
ShpmentId, City

Relationship acts as Foreign key to ShipmentId Primary Key
Is it so?

D) what 's the significance of name attribute of the adjacent annotation @Entity (name = "Orders2"). If i simply write @Entity for both of the classes, Persistenec is not happening
at all.






Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Gautam,

You are correct. I never use @Entity.name.

Hope it helps,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Gautam Ry
Ranch Hand

Joined: Dec 30, 2008
Posts: 41
Hi Reza
thanks for your response. But I didn't get you.
Should I write @Entity (name = "Orders2") or not?
If I write so, then what will be the value of name?
Will it be the table name where the entity will
be persisted?
Please, look into my other questions.
I am stuck.

Gautam


Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Gautam,

The entity name is defaulted to the unqualified name of the class. It is only needed to resolve class duplication issues, which never happens in real life.

Hope it helps,
Reza
 
 
subject: Please explain this @OneToOne