0

Mysql query

sql = new StringBuilder("SELECT * FROM table_name WHERE userId = :userId AND deleteFlag = 0 "
                    + "AND YEAR(NOW()) = YEAR(CreatedDate) ");
            sql.append("ORDER BY Id ");

            query = getSession().createSQLQuery(sql.toString());
            query.addEntity(table_name.class);
            query.setParameter("userId", userId);
Error performing load command: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.scotts.beans.model.User#4127404]
    at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:435) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:233) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1038) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:630) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.type.EntityType.resolve(EntityType.java:438) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:139) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:982) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:857) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2542) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.loader.Loader.list(Loader.java:2271) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1842) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at com.scotts.dao.impl.UserLawnProfileDAOImplV2.getLawnProfile(UserLawnProfileDAOImplV2.java:189) [classes:]

model

public class UserProgramRecommendationV2 implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    Integer id;

    @Column(name="UserId")
    private Long userId;
}

check in data base all rows matches with that defined in model.

4
  • that doesn;t solve it @Alien Commented Apr 11, 2020 at 18:35
  • It is failing only in queries where there is Id filed, I don't know whats wrong. and this started coming when I added an extra column in model. Commented Apr 11, 2020 at 18:37
  • @Column(name = "LawnCarePlanId") private Integer lawnCarePlanId; This was there is DB added in model mow @Alien Commented Apr 11, 2020 at 18:40
  • Let us continue this discussion in chat. Commented Apr 11, 2020 at 18:44

2 Answers 2

1

The problem is

User#4127404

Somewhere in the model you have a class X that have association to User with id 4127404. There is not a User with such id in the database.

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

2 Comments

This might be the case. I have below relationship in model @ManyToOne @JoinColumn(name = "UserId", insertable = false, updatable = false) private User user can I make some changes to run it when userId in user doesn't exist.
@DeepakPoojari You need to use FetchType.LAZY.
0

you use native query. check your database and name of UserId column. I think it's name is user_id, you should use this name in your query

2 Comments

The problem is not with other fields. But when i use ID (order by ID) it breaks with above error.
and this queries run in mysql perfectly but not using hibernate. @fatemeakbari

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.