简体   繁体   中英

Foreign key in hibernate

I have two simple tables Customers and Orders with relation oneToMany from customer to Orders table.

This is my Customers.java

@Entity
public class Customers implements Serializable {

@Id
@GeneratedValue
private int cID;
private String name;
private String email;

// getter and setters
}

And this is Orders.java :

@Entity
public class Orders implements Serializable {

@Id
@GeneratedValue
private int orderID;
private int cId;
@Column(nullable = false)
@Temporal(TemporalType.DATE)
private Date date;

@ManyToOne(cascade = CascadeType.ALL)
private Customers customers;
// getter and setters
}

Now, i am going to insert two record in Orders table:

    public static void main(String[] args) {
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Orders orders1 = new Orders();
    Orders orders2 = new Orders();
    Customers customer = new Customers();
    customer.setName("c1");
    customer.setEmail("abc@gmail.com");

    orders1.setDate(new Date());
    orders2.setDate(new Date());

    orders1.setCustomers(customer);
    orders2.setCustomers(customer);

    session.save(orders1);
    session.save(orders2);

    session.getTransaction().commit();
    session.close();
    sessionFactory.close();
}

This is the result in console:

Hibernate: alter table Orders drop foreign key FK_hmbx2rg9tsgqikb3kodqp90c4
Hibernate: drop table if exists Customers
Hibernate: drop table if exists Orders
Hibernate: create table Customers (cID integer not null auto_increment, email varchar(255), name varchar(255), primary key (cID))
Hibernate: create table Orders (orderID integer not null auto_increment, cId integer not null, date date not null, customers_cID integer, primary key (orderID))
Hibernate: alter table Orders add constraint FK_hmbx2rg9tsgqikb3kodqp90c4 foreign key (customers_cID) references Customers (cID)
Feb 24, 2015 1:58:52 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: insert into Customers (email, name) values (?, ?)
Hibernate: insert into Orders (cId, customers_cID, date) values (?, ?, ?)
Hibernate: insert into Orders (cId, customers_cID, date) values (?, ?, ?)

And this is the result tables:

在此处输入图片说明

Why the cID in Orders table (which is a foreign key references to customers ) is 0 ?

It should be 1 .

It think in your orders table customers_cId is the actual foreign key reference column to the customers table. As you haven't gave any column name explicitly, it internally took column name as customers_cId by joining the variables from both the entities. customers from the orders and cId from the customers entity.

Just to verify you can try giving some other name using @JoinColumn annotation.

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="order_cId") 
private Customers customers;

And cId in orders table is just one more independent column, as you have not set any value to it, its taking the default value as 0 . Try setting some random value to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM