Привет, я пишу простой код, который вставляет данные в две таблицы, используя jpa и версию Hibernate 3.2. Моя задача - сохранить данные сотрудника в двух таблицах, то есть Employee, который содержит employeeId, который является первичным ключом, которому назначено назначенное значение вручную, и адрес сотрудника. которые разделяют первичный ключ employeeId в таблице Employee с помощью аннотаций @onetoOne. Мои классы выглядят как
@Entity
@Table(name = "employee", catalog = "test", uniqueConstraints = {})
public class Employee implements java.io.Serializable {
// Fields
private Integer employeeid;
private String name;
private Employeeaddress employeeaddress;
// Constructors
/** default constructor */
public Employee() {
}
/** minimal constructor */
public Employee(Integer employeeid) {
this.employeeid = employeeid;
}
/** full constructor */
public Employee(Integer employeeid, String name) {
this.employeeid = employeeid;
this.name = name;
}
// Property accessors
@Id
@Column(name = "EMPLOYEEID", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getEmployeeid() {
return this.employeeid;
}
public void setEmployeeid(Integer employeeid) {
this.employeeid = employeeid;
}
@Column(name = "NAME", unique = false, nullable = true, insertable = true, updatable = true, length = 45)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne(mappedBy="employee")
@PrimaryKeyJoinColumn
public Employeeaddress getEmployeeaddress() {
return employeeaddress;
}
public void setEmployeeaddress(Employeeaddress employeeaddress) {
this.employeeaddress = employeeaddress;
}
}
@Entity
@Table(name = "employeeaddress", catalog = "test", uniqueConstraints = {})
public class Employeeaddress implements java.io.Serializable {
// Fields
private Integer employeeid;
private String address;
private Employee employee;
// Constructors
/** default constructor */
public Employeeaddress() {
}
/** minimal constructor */
public Employeeaddress(Integer employeeid) {
this.employeeid = employeeid;
}
/** full constructor */
public Employeeaddress(Integer employeeid, String address) {
this.employeeid = employeeid;
this.address = address;
}
// Property accessors
@Id
@Column(name = "EMPLOYEEID", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getEmployeeid() {
return this.employeeid;
}
public void setEmployeeid(Integer employeeid) {
this.employeeid = employeeid;
}
@Column(name = "ADDRESS", unique = false, nullable = true, insertable = true, updatable = true, length = 45)
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
@OneToOne(mappedBy="employeeaddress")
@PrimaryKeyJoinColumn
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
и мой основной класс
public class EmployeeDao {
public static void main(String[] args) {
try{
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.addAnnotatedClass(employeeforms.Employee.class);
cfg.addAnnotatedClass(employeeforms.Employeeaddress.class);
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session =sessionFactory.openSession();
Employee e = new Employee();
e.setEmployeeid(100872);
e.setName("sandeep");
Employeeaddress ed = new Employeeaddress();
ed.setAddress("Hyderabad");
e.setEmployeeaddress(ed);
session.save(e);
}catch (Exception e) {
e.printStackTrace();
}
}
}
когда я запускаю свой основной класс, я получаю исключения ниже
java.lang.NullPointerException
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at employeeDao.EmployeeDao.main(EmployeeDao.java:16)
мои столы
CREATE TABLE employee (
EMPLOYEEID int(10) unsigned NOT NULL ,
NAME varchar(45) default '',
PRIMARY KEY (EMPLOYEEID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE employeeaddress (
EMPLOYEEID int(10) unsigned NOT NULL default '0',
ADDRESS varchar(45) default '',
PRIMARY KEY (EMPLOYEEID)
) ENGINE=InnoDB
DEFAULT CHARSET=latin1;