Announcement Announcement Module
Collapse
No announcement yet.
Query produced by Spring Data - OneToOne mapping Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Query produced by Spring Data - OneToOne mapping

    I am analyzing query produced by Spring Data for my model:
    Code:
    public class Customer {
    
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private Short id;
    	
    	@Version
    	Timestamp version;
    
    	String name;
    	
    	@OneToOne
    	Description description;
    	
    	@OneToMany(mappedBy="customer")
    	List<CustomerOrder> order;
    	
    	@ManyToMany(mappedBy="customers")
    	Set<Address> addresses; 
    	
    	@ManyToOne
    	WorkPlace workPlace;
    }
    
    public class Description {
    
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private Short id;
    	
    	@Version
    	Timestamp version;
    
    	@OneToOne(mappedBy="description") @Getter @Setter
    	Customer customer;
    	
    	@Getter @Setter
    	String description;
    }
    
    public class WorkPlace {
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private Short id;
    	
    	@Version
    	Timestamp version;
    	
    	@Getter @Setter
    	String workplaceName;
    	
    	@OneToMany(mappedBy="workPlace")
    	Set<Customer> customers;
    
    }
    
    Probably not important part:
    
    public class Address {
    
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private Short id;
    	
    	@Version
    	Timestamp version;
    
    	@ManyToMany @Getter
    	List<Customer> customers = new LinkedList<>();
    	
    	@Getter @Setter
    	String street;
    }
    
    public class CustomerOrder {
    
    	@Id
    	@GeneratedValue
    	private Short id;
    	
    	@Version
    	Timestamp version;
    
    	@ManyToOne @Getter @Setter
    	Customer customer;
    	
    	@Getter @Setter
    	int quantity;
    }
    I have Spring Data CRUD repository for Customer.
    After calling: repo.findOne((short)1) the following queries are produced:
    Code:
    select customer0_.id as id11_2_, customer0_.description_id as descript4_11_2_, customer0_.name as name11_2_, customer0_.version as version11_2_, customer0_.workPlace_id as workPlace5_11_2_, descriptio1_.id as id12_0_, descriptio1_.description as descript2_12_0_, descriptio1_.version as version12_0_, workplace2_.id as id15_1_, workplace2_.version as version15_1_, workplace2_.workplaceName as workplac3_15_1_ 
    from Customer customer0_ left outer join 
         Description descriptio1_ on customer0_.description_id=descriptio1_.id left outer join 
         WorkPlace workplace2_ on customer0_.workPlace_id=workplace2_.id 
    where customer0_.id=?
    
    select customer0_.id as id11_2_, customer0_.description_id as descript4_11_2_, customer0_.name as name11_2_, customer0_.version as version11_2_, customer0_.workPlace_id as workPlace5_11_2_, descriptio1_.id as id12_0_, descriptio1_.description as descript2_12_0_, descriptio1_.version as version12_0_, workplace2_.id as id15_1_, workplace2_.version as version15_1_, workplace2_.workplaceName as workplac3_15_1_ 
    from Customer customer0_ left outer join 
         Description descriptio1_ on customer0_.description_id=descriptio1_.id left outer join 
         WorkPlace workplace2_ on customer0_.workPlace_id=workplace2_.id 
    where customer0_.description_id=?
    Why do we need the second query? The first one is not enough?
    Am I missing something, or the same query (same result)is called twice?
    Thanks,
    Pawel
Working...
X