1: one-to-one, one-way, foreign key Association
The Student and stusentIDCard tables have one-to-one relationship, that is, one Student has only one Student ID card and one Student ID card corresponds to only one Student.
The Stuent table has a foreign key. For details, refer to StudentIDCard.
The performance class is to aggregate a stusentIDCard in the Student instance.
[Java]
@ Entity
Public class Student
{
Private int id;
Private String name;
Private int age;
Private StudentIDCard cardId;
@ Id
@ GeneratedValue // you can set a separate auto-incrementing sequence, which shares one
Public int getId (){
Return id;
}
@ OneToOne // one-to-one relationship
@ JoinColumn (name = "card_Id") // specify that the foreign key name is card_Id
Public StudentIDCard getCardId (){
Return cardId;
}
[Java]
@ Entity
Public class StudentIDCard
{
Private int id;
Private String kinds;
Private Date datePublished;
@ Id
@ GeneratedValue // you can define a separate auto-incrementing sequence.
Public int getId (){
Return id;
}
Test class
[Java]
@ Test
Public void testIN (){
Session session = HibernateUtil. getSessionFactory (). getCurrentSession ();
Session. beginTransaction ();
StudentIDCard sic = new StudentIDCard ();
Sic. setKinds ("primarySchool ");
Sic. setDatePublished (new Date ());
Session. save (sic );
Student s = new Student ();
S. setName ("Li Si ");
S. setAge (0 );
S. setCardId (sic );
Session. save (s );
Session. getTransaction (). commit ();
HibernateUtil. getSessionFactory (). close ();
}
[Java]
@ Test
Public void testShow (){
New SchemaExport (new AnnotationConfiguration (). configure (). create (false, true );
}
Table creation result:
Foreign key (card_Id) references StudentIDCard
16:15:13, 184 DEBUG SchemaUpdate: 226-create table Student (id number (255) not null, age number () not null, name varchar2 (char ), card_Id number (10, 0), primary key (id ))
16:15:13, 193 DEBUG SchemaUpdate: 226-create table StudentIDCard (id number (255) not null, datePublished timestamp, kinds varchar2 (char), primary key (id ))
16:15:13, 200 DEBUG SchemaUpdate: 226-alter table Student add constraint FKF3371A1BEA113360foreign key (card_Id) references StudentIDCard
The reverse engineering diagram in PowerDesigner is as follows:
The fields in the Student table are to be referenced in the StudenIDCard table. The foreign key name I specified is: "CARD_ID"
2: one-to-one, bidirectional, foreign key Association
Two-way performance is manifested in the class: Student and stusentIDCard aggregate each other to an instance,
@ OneToOne
The Student class remains unchanged. Refer to the one above and leave it blank.
[Java]
@ Entity
Public class StudentIDCard
{
Private int id;
Private String kinds;
Private Date datePublished;
Private Student student;
@ Id
@ GeneratedValue
Public int getId (){
Return id;
}
@ OneToOne // mutual possession, with OneToOne added
Public Student getStudent (){
Return student;
}
This means that duplicate fields will appear in the table, and mappedBy must be set to indicate that the other party is dominant and redundancy is eliminated.
On getStudent () of the StudentIDCard class
@ OneToOne (mappedBy = "StudentIDCard") indicates that the other party is dominant, and the "cardId" attribute of the other party (Student) has been mapped to eliminate the redundancy of Foreign keys.
GetStudent ()
[Java]
@ OneToOne (mappedBy = "cardId") // indicates that the other party is dominant, and the "cardId" attribute of the other party (Student) has been mapped to eliminate the redundancy of Foreign keys.
Public Student getStudent (){
Return student;
}
3: one-to-one, one-way, primary key Association
Replace @ JoinColumn with @ PrimaryKeyJoinColumn // specify the foreign key name
[Java] view plaincopy
Replace @ JoinColumn with @ PrimaryKeyJoinColumn // specify the foreign key name