英文:
I am trying to mapping three entity with another entity by using onetoone annotation
问题
The issue with the data not being inserted into the address table may be due to a missing save operation for the "address" entity in your code. You need to explicitly save the "address" entity before committing the transaction. Here's the corrected code:
public class OneToOneApp {
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.configure("o2omapping.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Employee employee = new Employee();
employee.setName("Iqra");
employee.setEmail("iqra@gmail.com");
Address address = new Address();
address.setStreet("Manda Road");
address.setCity("Allahabad");
address.setCountry("India");
Info info = new Info();
info.setGender("FeMale");
info.setPhone("12334567");
employee.setInfo(info);
info.setEmp(employee);
address.setEmp(employee);
// Save the address entity
session.save(address);
session.save(employee); // Save the employee entity
transaction.commit();
session.close();
}
}
By adding session.save(address);
before session.save(employee);
, you ensure that both the "address" and "employee" entities are saved to the database, which should resolve the issue of data not being inserted into the address table.
英文:
I have three entity employee , info, and address , i want mapped employee entity with info and address entity by using onetoone mapping by using hibernate and jpa annotation.
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int empId;
private String name;
private String email;
@OneToOne(mappedBy = "emp", cascade = CascadeType.ALL)
private Info info;
@OneToOne(mappedBy = "emp", cascade = CascadeType.ALL)
private Address address;
//getter and setter
@Entity
public class Info {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int infoId;
private String gender;
private String phone;
@OneToOne
@JoinColumn(name = "emp_id")
private Employee emp;
//getter and setter
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int addrId;
private String street;
private String city;
private String country;
@OneToOne
@JoinColumn(name = "emp_id")
private Employee emp;
//getter and setter
public class OneToOneApp {
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.configure("o2omapping.cfg.xml");
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Employee employee =new Employee();
employee.setName("Iqra");
employee.setEmail("iqra@gmail.com");
Address address = new Address();
address.setStreet("Manda Road");
address.setCity("Allahabad");
address.setCountry("India");
Info info = new Info();
info.setGender("FeMale");
info.setPhone("12334567");
employee.setInfo(info);
info.setEmp(employee);
address.setEmp(employee);
session.save(employee);
transaction.commit();
session.close();
}
}
alter table Info
add constraint FKdjqy16j48ffa2x19eqgx3o8f0
foreign key (emp_id)
references Employee (empId)
Hibernate:
insert
into
Employee
(email, name)
values
(?, ?)
Hibernate:
insert
into
Info
(emp_id, gender, phone)
values
(?, ?, ?)
data not Inserted into address table
why??
答案1
得分: 0
他在说实话。您已经在customer类中使用一对一关系依赖设置了地址对象,但您没有设置其信息。您应该在employee.setAddress()
之后添加这一行,employee.setInfo()
。
英文:
he is telling the truth. You have set the address object in the customer class with a One-to-One relationship dependency, but you have not set its information. You should add this after the line employee.setAddress()
, employee.setInfo()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论