I'm using JPA2 and Springboot2X. Trying to define a simple one-to-one relationship in User and Account tables.
User is the parent while Account is the child. The relationship is from parent to child. Followed this.
User table has a column account_no to hold the account number which is the primary key of the Account table. So User holds the foreign key.
@Entity
public class Account extends AbstractEntity implements Serializable{
// fields like account#, balance etc goes here..
@OneToOne (fetch = FetchType.LAZY)
private User user;
}
@Entity
public class User extends AbstractEntity implements Serializable{
// Other fields related to user entity go here ..
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "account_no", referencedColumnName = "account_number")
private Account account;
}
Saving is working properly. But when I fetch a user ( User user = userRp.findByUserID(userid);), I get data from both tables. My question is when I define fetch = FetchType.LAZY why the account table data is fetching? I only want the user data to be in the output as when I run the select query in the User table.
Output :
{
"id": 1,
"userID": "xxxxx",
"firstName": "Dsd",
"llastName": "cccc",
"email": "danqsaa@brown",
"creation_date": "2021-12-19 19:41",
"modified_date": "2021-12-19 19:41",
"account": {
"id": 2,
"accountNo": 6848982326,
"balance": 4000.0,
"creationUser": "xxx",
"creation_date": "2021-12-19 19:41",
"modified_date": "2021-12-19 19:41",
"transactions": []
}
}
Added below RestController, Service, and Repo details.
@RestController
public class UserController {
@GetMapping("/users/{userID}")
public ResponseEntity<Object> getUser(@PathVariable("userID") String userID){
return new ResponseEntity<>(userServiceImpl.getUserByID(userID), HttpStatus.OK));
}
}
@Service
public class UserService {
// injected user repo
public User getUserByID(String userID){
return userRepo.findByUserID(userID);
}
}
public interface UsersRepository extends CrudRepository<User, String> {
User findByUserID(String userID);
}

