I have a class User. User class have name field and Address fields. Address is another class. Adress class have 3 fields, name, zipcode and City. City is another class. I want to query User but I do not want to generate any query on City class. Chatgpt give me this code sample, but it did not work. It gives me this suggestion; "It is not possible to exclude sub entities when querying the desired entity in Hibernate by taking advantage of the lazy loading mechanism." I want to be sure if it is right.
Best Regards,
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
criteria.createAlias("adress", "adress", JoinType.LEFT_OUTER_JOIN);
criteria.createCriteria("adress.city", JoinType.LEFT_OUTER_JOIN).setProjection(null);
criteria.setProjection(Projections.property("id"), Projections.property("name"), Projections.property("adress.id"), Projections.property("adres.name"), Projections.property("adress.zipcode"));
criteria.setResultTransformer(Transformers.aliasToBean(User.class));
List<User> results = (List<User>) hibernateTemplate.findByCriteria(criteria);