Deepak Poojari
Deepak Poojari

Reputation: 766

Error in Mysql query using hibernate and java

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.

Upvotes: 0

Views: 89

Answers (2)

v.ladynev
v.ladynev

Reputation: 19976

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.

Upvotes: 1

fatemeakbari
fatemeakbari

Reputation: 331

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

Upvotes: 0

Related Questions