我正在尝试使用onetoone注解将三个实体映射到另一个实体。

huangapple go评论62阅读模式
英文:

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().

huangapple
  • 本文由 发表于 2023年5月7日 14:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定