4

I am using Spring Data JPA and Hibernate as a provider. I've created several Repository classes which extends to JPARepository<Entity,Serializable> class. I am failing at the moment when I am fetching one entity it brings attached / connected entities along with it ! which are either connected via @OneToOne @OneToMany etc. How can I avoid fetching those connected entities ?
I have tried with @OneToMany(fetch=FetchType.LAZY) etc but still no luck. Following are my java code:
Repository

public interface TicketRepository extends JpaRepository<Ticket, Integer>{

}  

Ticket Entity

@Entity
@Table(name = "tbl_tickets")
public class Ticket {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "customer", nullable = false, length = 256)
    private String customer;


    @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    @JoinColumn
    private User creator; 

    // ... other properties
}  

Service

@Service
public class TicketService {

  public Ticket save(Ticket obj,String id) {
       User user = userService.findById(Integer.valueOf(id));
       obj.setCreator(user);
       Ticket savedTicket = ticketRepository.save(obj);
   }
}  

savedTicket always fetches User entity as well which I do not want to. How could I achieve this ?
enter image description here

Thanks

3
  • Your code is fetching an user object and setting "creator" on ticket and you expect creator to be null?? Commented Jan 4, 2015 at 11:40
  • @RC. you are right, because of mapping I have to set it like that ! may be the way I mapped is wrong ! any idea on mapping part ? Commented Jan 4, 2015 at 11:48
  • there are problems with lazy fetching of OneToOne (perhaps solved in the answer of Harshal Patil below), however you should have no problems with lazy fetching of OneToMany(fetch=FetchType.LAZY). If the field gets initialized with such annotation, this might mean you access the property annotated with @OneToMany (which should be a set or a list etc.) somewhere before that. Commented Jan 5, 2015 at 8:42

1 Answer 1

1

Get Lazy loading working on nullable one-to-one mapping you need to let hibernate do Compile time instrumentation and add a @LazyToOne(value = LazyToOneOption.NO_PROXY) to the one-to-one relation.

@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn
@LazyToOne(value = LazyToOneOption.NO_PROXY)
private User creator;

Hope this will work.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, for late reply, I added changes suggested by you, but it says could not initialize proxy - no Session
@agpt Add Transactional annotation over method where your are retrieving creator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.