Abhishek Patil
Abhishek Patil

Reputation: 15

Object relation mapping

I have an Employee class with data members as follows:

@Id
private int empId;
private String empName;
private String department;
private String baseLocation;
@OneToOne
@JoinColumn(name="address_id")
private Address address;

and an Address class:

@Id
private int address_id;
private String city;
private String pincode;

When I store an Employee object into the database, the Address object is stored as null, even though the proper Address object is passed as an argument.

I am using JpaRepository with springboot. My method is as follows

@Autowired
EmployeeServiceImpl empService;

public static void main(String[] args) {
    SpringApplication.run(EmployeeManagementApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
    Address address = new Address(5,"pun","591237");
    Employee emp = new Employee(11, "Abc", "cse", "pune", address);
    System.out.println("ghii");
    
    try {
        service.insertAddress(address);
        empService.AddEmployee(emp);
        //invoke service layer method to insert Customer
        System.out.println("Records are successfully added..");
        
          System.out.println("reading"); 
          Employee emploee = empService.getEmployee(11);
          System.out.println(emploee.getDepartment());
         
    }
    catch(Exception e) {
        System.out.println("in exception");
        e.printStackTrace();
    }

Please help me to store the objects into the database.

Upvotes: 0

Views: 68

Answers (1)

Anton Shelenkov
Anton Shelenkov

Reputation: 325

Try

@OneToOne(cascade = {CascadeType.ALL})

More about cascading: here

Upd: Detailed classes (using lombok for entities):

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
    @Id
    private int empId;
    private String empName;
    private String department;
    private String baseLocation;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="address_id")
    private Address address;
}

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address {
    @Id
    private int address_id;
    private String city;
    private String pincode;
}

public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
}

@SpringBootApplication
@ConfigurationPropertiesScan
public class Application implements CommandLineRunner {

    @Autowired
    private EmployeeRepository employeeRepository;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Address address = new Address(5, "pun", "591237");
        Employee emp = new Employee(11, "Abc", "cse", "pune", address);

        try {
            System.out.println("Start");
            employeeRepository.save(emp);
            System.out.println("Records are successfully added..");

            System.out.println("reading");
            Employee emploee = employeeRepository.findById(11).orElse(null);
            System.out.println(emploee.getDepartment());
            System.out.println(emploee.getAddress().getCity());

        } catch (Exception e) {
            System.out.println("in exception");
            e.printStackTrace();
        }
    }
}

Output:

Start
Hibernate: select employee0_.emp_id as emp_id1_2_1_, employee0_.address_id as address_5_2_1_, employee0_.base_location as base_loc2_2_1_, employee0_.department as departme3_2_1_, employee0_.emp_name as emp_name4_2_1_, address1_.address_id as address_1_1_0_, address1_.city as city2_1_0_, address1_.pincode as pincode3_1_0_ from employee employee0_ left outer join address address1_ on employee0_.address_id=address1_.address_id where employee0_.emp_id=?
Hibernate: select address0_.address_id as address_1_1_0_, address0_.city as city2_1_0_, address0_.pincode as pincode3_1_0_ from address address0_ where address0_.address_id=?
Hibernate: insert into address (city, pincode, address_id) values (?, ?, ?)
Hibernate: insert into employee (address_id, base_location, department, emp_name, emp_id) values (?, ?, ?, ?, ?)
Records are successfully added..
reading
Hibernate: select employee0_.emp_id as emp_id1_2_0_, employee0_.address_id as address_5_2_0_, employee0_.base_location as base_loc2_2_0_, employee0_.department as departme3_2_0_, employee0_.emp_name as emp_name4_2_0_, address1_.address_id as address_1_1_1_, address1_.city as city2_1_1_, address1_.pincode as pincode3_1_1_ from employee employee0_ left outer join address address1_ on employee0_.address_id=address1_.address_id where employee0_.emp_id=?
cse
pun

Upvotes: 2

Related Questions