This is my code:
The tables in sql server:
create table Department(
Department_ID int identity primary key,
Department_Name varchar(20) not null,
);
create table Employee(
Employee_ID int identity primary key,
Employee_Name varchar(20) not null,
Department_ID int not null
);
alter table Employee add constraint FK_Employee foreign key(Department_ID) references dbo.Department;
Department.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.yp.entity2">
<class name="Department" table="Department">
<id name="id" column="Department_ID">
<generator class="native"/>
</id>
<property name="name" column="Department_Name" length="20" />
<set name="emps" inverse="true" lazy="true">
<key column="Department_ID"/>
<one-to-many class="Employee"/>
</set>
</class>
</hibernate-mapping>
Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.yp.entity2">
<class name="Employee" table="Employee">
<id name="id" column="Employee_ID">
<generator class="native"/>
</id>
<property name="name" column="Employee_Name" length="20"/>
<many-to-one name="dept" class="Department" column="Department_ID"/>
</class>
</hibernate-mapping>
Entity class
public class Employee {
private Integer id;
private String name;
private Department dept;
//getter,setter
}
public class Department {
private Integer id;
private String name;
private Set<Employee> emps = new HashSet<>();
//getter,setter
}
My problem is I have already created the foreign key named “FK_Employee” in Employee table, but the client program still generates a new foreign key named “FKj9uwop98sf53kym2p5719ajt2” in table Employee.
What should I do so the client program does not need to change the table structure in sql server, Did I missed something in Employee.hbm.xml, like declare the foreign key name in mapping file?