I have two classes User and Application.
User has one to many mapping to application. User has a lot of fields, some are pretty big ones like description. I don't want to load all the fields of the user when I want to use it to see the relationship between user and application so I created a proxy class that has username and a collection of application. How do I the two tables to this class? Or what is the best practice?
update :
#Entity
public class UserProfile implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String userName;
private String password;
#OneToMany
#JoinTable(name = "userapplicationlink", joinColumns = #JoinColumn(name = "userId"), inverseJoinColumns = #JoinColumn(name = "appId"))
private Collection<Application> applications;
}
#Entity
public class Application
{
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
private String name;
private String url;
}
main()
{
UserProfile user1 = new UserProfile();
user1.setUserName("sasd");
user1.setPassword("123");
Application app = new Application();
app.setName("User Application");
app.setPriority(1);
app.setUrl("/user.do");
app.setDescription("app");
user1.setApplications(new ArrayList());
user1.getApplications().add(app);
SessionFactory sessionFac = new Configuration().configure().buildSessionFactory();
Session session = sessionFac.openSession();
session.beginTransaction();
session.save(user1);
session.save(app);
session.getTransaction().commit();
System.out.println("End");
session.close();
sessionFac.close();
sessionFac = new Configuration().configure().buildSessionFactory();
session = sessionFac.openSession();
session.beginTransaction();
try
{
Query query = session.createQuery("SELECT NEW empire.erp.server.db.UserNameAndApplications(u.userName, u.applications) FROM UserProfile u JOIN FETCH u.applications");
List result = query.list();
session.getTransaction().commit();
session.close();
}
finally
{
session.close();
sessionFac.close();
}
}
Stack trace
Exception in thread "main" org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=empire.erp.server.db.UserProfile.applications,tableName=Application,tableAlias=applicatio2_,origin=UserProfile userprofil0_,columns={userprofil0_.id ,className=empire.erp.server.db.Application}}] [SELECT NEW empire.erp.server.db.UserNameAndApplications(u.userName, u.applications) FROM empire.erp.server.db.UserProfile u JOIN FETCH u.applications]
at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:76)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1907)
at empire.erp.server.db.UserProfile.main(UserProfile.java:216)
Caused by: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=empire.erp.server.db.UserProfile.applications,tableName=Application,tableAlias=applicatio2_,origin=UserProfile userprofil0_,columns={userprofil0_.id ,className=empire.erp.server.db.Application}}]
at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:214)
at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:991)
at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:759)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:675)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:311)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:259)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
... 8 more
Update 2: running query without fetch
Hibernate: select userprofil0_.userName as col_0_0_, . as col_1_0_ from UserProfile userprofil0_ inner join userapplicationlink applicatio1_ on userprofil0_.id=applicatio1_.userId inner join Application applicatio2_ on applicatio1_.appId=applicatio2_.id inner join userapplicationlink applicatio3_ on userprofil0_.id=applicatio3_.userId inner join Application applicatio4_ on applicatio3_.appId=applicatio4_.id
May 07, 2016 4:17:21 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42601
May 07, 2016 4:17:21 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: syntax error at or near "."
Position: 43
May 07, 2016 4:17:21 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:postgresql://localhost:5432/optimalyou]
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2115)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1898)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1874)
at org.hibernate.loader.Loader.doQuery(Loader.java:919)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doList(Loader.java:2610)
at org.hibernate.loader.Loader.doList(Loader.java:2593)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2422)
at org.hibernate.loader.Loader.list(Loader.java:2417)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1339)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
at empire.erp.server.db.UserProfile.main(UserProfile.java:217)
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "."
Position: 43
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:161)
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:114)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
... 15 more
Had to use an alias for the collection. Not sure why though. The proper HQL is
SELECT NEW empire.erp.server.db.UserNameAndApplications(u.userName, app) FROM UserProfile u JOIN u.applications app
Related
i'm trying to execute this query inside the spring boot repository class , but console shows the error ' column id not found ' also the postman shows:
"status": 500,
"error": "Internal Server Error",
"message": "could not execute query; SQL [SELECT etablissement.etab_name , app_user.creatdate_time FROM etablissement JOIN app_user WHERE year(app_user.creatdate_time)= ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query",
QUERY REPOSITORY
#Query(nativeQuery=true, value="SELECT etablissement.etab_name , app_user.creatdate_time FROM etablissement JOIN app_user WHERE year(app_user.creatdate_time)= :year")
public List<User> findALLUserByyear(#Param("year") String year);
CONTROLLER
#GetMapping(value="/etablissementAlls/{year}")
public EtablissementDto EtabDTOALL(#PathVariable String year) {
EtablissementDto a = new EtablissementDto();
a.setUsers(userRepository.findALLUserByyear(year));
return a;
}
Stack Trace
2021-05-05 11:14:17.600 WARN 5240 --- [nio-8020-exec-2] org.club.config.JwtRequestFilter : JWT Token does not begin with Bearer String
2021-05-05 11:14:17.604 WARN 5240 --- [nio-8020-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: S0022
2021-05-05 11:14:17.604 ERROR 5240 --- [nio-8020-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Column 'id' not found.
2021-05-05 11:14:17.606 ERROR 5240 --- [nio-8020-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [SELECT etablissement.etab_name , app_user.creatdate_time FROM etablissement JOIN app_user WHERE year(app_user.creatdate_time)= ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
java.sql.SQLException: Column 'id' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) ~[mysql-connector-java-5.1.46.jar:5.1.46]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) ~[mysql-connector-java-5.1.46.jar:5.1.46]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) ~[mysql-connector-java-5.1.46.jar:5.1.46]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861) ~[mysql-connector-java-5.1.46.jar:5.1.46]
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1080) ~[mysql-connector-java-5.1.46.jar:5.1.46]
NOTE
I tested this query in MYSQL PHPmyAdmin, and it works fine
USER ENTITY
#Entity
#Table(name = "app_user")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name = "is_active")
private boolean active;
#JsonIgnore
#ManyToMany(fetch = FetchType.EAGER)
#Fetch(value = FetchMode.SUBSELECT)
#JoinTable(name = "user_etablissement", joinColumns
= #JoinColumn(name = "user_id",
referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "etablissement_id",
referencedColumnName = "id"))
private List<Etablissement> etablissements;
Clarify few things:
The MYSQL PHPmyAdmin you have queried on and the service you are connecting database to are pointing to the same database server?
Have you checked manually if 'id' column is created in the tables you are querying to?
You should change your query if you have create_time field in User entity
#Query(value="SELECT u from User u where year(u.creatdate_time)=:year")
public List<User> findALLUserByyear(#Param("year") String year);
Today i was trying to add Customer to database using Hibernate Annotation but i dont know why i am facing a reference Problem with a table !
Please read the Exception below
ERROR: Referential integrity constraint violation:
"FKOFMCQE0O4K2TFOXB308SKTMQ3: PUBLIC.CUSTOMER FOREIGN
KEY(CUS_BILLINGADDRESSID) REFERENCES
PUBLIC.CUSTOMERBILLINGADDRESS(CUS_BILLINGADDRESSID) ('CBA00001')"; SQL
statement:
update Customer set cus_emailid=?, cus_mobileno=?, cus_name=?, cus_billingaddressid=?, cus_cartid=?, cus_loginid=?,
cus_shippingaddressid=? where cus_id=? [23506-193]
Apr 01, 2017 7:09:57 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Apr 01, 2017 7:09:57 PM org.hibernate.internal.ExceptionMapperStandardImpl
mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not
execute statement]
Apr 01, 2017 7:09:57 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/TechNXT] threw exception [Request processing failed; nested
exception is javax.persistence.PersistenceException:
org.hibernate.exception.ConstraintViolationException: could not
execute statement] with root cause
org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FKOFMCQE0O4K2TFOXB308SKTMQ3: PUBLIC.CUSTOMER FOREIGN
KEY(CUS_BILLINGADDRESSID) REFERENCES
PUBLIC.CUSTOMERBILLINGADDRESS(CUS_BILLINGADDRESSID) ('CBA00001')"; SQL
statement:
update Customer set cus_emailid=?, cus_mobileno=?, cus_name=?, cus_billingaddressid=?, cus_cartid=?, cus_loginid=?,
cus_shippingaddressid=? where cus_id=? [23506-192]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.constraint.ConstraintReferential.checkRowOwnTable(ConstraintReferential.java:372)
at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:314)
at org.h2.table.Table.fireConstraints(Table.java:967)
at org.h2.table.Table.fireAfterRow(Table.java:985)
at org.h2.command.dml.Update.update(Update.java:151)
at org.h2.command.CommandContainer.update(CommandContainer.java:98)
at org.h2.command.Command.executeUpdate(Command.java:258)
at org.h2.server.TcpServerThread.process(TcpServerThread.java:344)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:158)
at java.lang.Thread.run(Unknown Source)
Here is my Two model Tables
NOTE : i have generated getters and Setters but not posting so making as small as possible
1. Customer table which has a references of the other table
#Entity
public class Customer {
#Id
private String cus_id;
private String cus_name;
private String cus_emailid;
private String cus_mobileno;
#OneToOne(cascade=CascadeType.REFRESH)
#JoinColumn(name="cus_loginid")
private CustomerDetails customerdetails;
#OneToOne(cascade=CascadeType.REFRESH)
#JoinColumn(name="cus_billingaddressid")
private CustomerBillingAddress customerbillingaddress;
#OneToOne(cascade=CascadeType.REFRESH)
#JoinColumn(name="cus_shippingaddressid")
private CustomerShippingAddress customershippingaddress;
#OneToOne(cascade=CascadeType.REFRESH)
#JoinColumn(name="cus_cartid")
private CustomerCart customercart;
My Customer Billing Address Model
#Entity
public class CustomerBillingAddress {
#Id
private String cus_billingaddressid;
private String cus_houseno;
private String cus_street;
private String cus_area;
private String cus_city;
private String cus_state;
private String cus_country;
private String cus_pincode;
#OneToOne(mappedBy="customerbillingaddress")
private Customer customer;
Similarly Other Models
And Finally My DAO method which add the Customer to db
here is the code !
#Transactional
public String addCustomer(Customer customer) {
System.out.println("CustomerDao -TechNXT\n");
Session ses = sf.openSession();
customer.setCus_id(generateCustomerid());
customer.setCustomerbillingaddress(new CustomerBillingAddress());
customer.setCustomershippingaddress(new CustomerShippingAddress());
customer.setCustomercart(new CustomerCart());
customer.getCustomerdetails().setCus_loginid(generateCustomerLoginid());
customer.getCustomerbillingaddress().setCus_billingaddressid(generateCustomerBillingid());
customer.getCustomershippingaddress().setCus_shippingaddressid(generateCustomershippingid());
customer.getCustomercart().setCus_cartid(generateCustomerCartid());
customer.getCustomerdetails().setCus_isenabled(true);
customer.getCustomerdetails().setCus_role("ROLE_USER");
Transaction tr = ses.beginTransaction();
ses.save(customer);
tr.commit();
ses.close();
return customer.getCustomerdetails().getCus_loginid();
}
Well i have being trying stuffs since 3hrs but failed to get a solution of it !!
Sorry for taking your precious time.
and Thanking you in advance for helping me !
can you mark the #Table annontation on your class
like
#Table(name ="Customer")
I'm getting this error quite alot, and my hibernate is acting strange when I try to insert objects into my tables in my mySQL database.
okt. 06, 2015 9:57:28 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1062, SQLState: 23000
okt. 06, 2015 9:57:28 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Duplicate entry '77' for key 'UK_2gy9x2d1hqmk0d77o8ux44c6o'
okt. 06, 2015 9:57:28 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
org.hibernate.exception.ConstraintViolationException: could not execute statement
When I look in MySQL WOrkbench I can see that whenever I persist or merge I get duplicate rows in my database tables.
I use these two methods for persist and merge:
public void persist(Object entity) {
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.persist(entity);
session.flush();
session.getTransaction().commit();
}
public void merge(Object entity) {
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.merge(entity);
session.flush();
session.getTransaction().commit();
}
I got my user class which has a ManyToMany with note class.
#ManyToMany( fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST } )
#JoinTable(
name = "user_note",
joinColumns = {#JoinColumn(name = "empId")},
inverseJoinColumns = {#JoinColumn(name = "note_id")})
private Set<Note> bookmarks = new HashSet<>();
note:
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private long projectId;
and a third class which has a OneToMany relation with note.
#OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
#JoinTable(name = "organisation_note", joinColumns = { #JoinColumn(name = "OrganisationId") }, inverseJoinColumns = { #JoinColumn(name = "note_id") })
private Set<Note> bookmarks = new HashSet<>();
I've been looking around, and can't seem to find any topics on duplicated rows in the database. The first persist won't duplicate the row, but the second will, and the third will throw the exception. After that it's just exceptions after each time I try to persist. Which is not that reliable. Any input is appreciated.
1-1 association:
#OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "messageDetails")
public MessageEntry getMessageEntry() {
return messageEntry;
}
#OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public MessageDetails getMessageDetails() {
return messageDetails;
}
Want to load dependent entry in stateless session:
MessageDetails messageDetails =
(MessageDetails) sess.createCriteria(MessageDetails.class)
.add(Restrictions.eq("messageEntry", messageEntry))
.uniqueResult();
I get
Exception in thread "main" org.hibernate.exception.SQLGrammarException: No value specified for parameter 1
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
The SQL is not what I want. May be I don't understand something?
03:21:13,274 DEBUG SQL:104 - /* criteria query */ select this_.id as id11_0_, this_.date as date11_0_, this_.messageId as messageId11_0_, this_.replyTo as replyTo11_0_, this_.subject as subject11_0_ from MessageDetails this_ where this_.id=?
I want to select MessageDetails by its property messageEntry, isn't that possible? Thanx.
I'm having a hard time constructing a criteria query to get the "permissions" attribute from the Role entity with id = 2. The permission attribute is of Set type, so I'm creating a join and selecting from it, but the query fails with invalid grammar exception reporting "Unknown column '2L' in 'where clause'"
The criteria query that generates the error was built this way:
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery();
Class<?> queryScopeClass = temp.pack.commons.user.Role.class;
Root<?> from = criteriaQuery.from(queryScopeClass);
Path<?> idAttrPath = from.get("id");
// also tried criteriaBuilder.equal(attributePath, new Long(2))
Predicate predicate = criteriaBuilder.equal(attributePath, criteriaBuilder.literal(new Long(2)))
criteriaQuery.where(predicate);
Path<?> attributePath = from.get("permissions");
PluralAttributePath<?> pluralAttrPath = (PluralAttributePath<?>)attributePath;
PluralAttribute<?, ?, ?> pluralAttr = pluralAttrPath.getAttribute();
Join<?, ?> join = from.join((SetAttribute<Object,?>)pluralAttr);
TypedQuery<Object> typedQuery = entityManager.createQuery(criteriaQuery.select(join));
return (List<P>)typedQuery.getResultList();
When I execute the line to actually return the results, the following exception is thrown:
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1235)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:250)
at org.hibernate.ejb.criteria.CriteriaQueryCompiler$3.getResultList(CriteriaQueryCompiler.java:260)
at temp.pack.dao.impl.DefaultDAOService.getProperties(DefaultDAOService.java:628)
...
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2452)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
at org.hibernate.loader.Loader.list(Loader.java:2187)
at org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:936)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:241)
... 20 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '2L' in 'where clause'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2264)
at com.jolbox.bonecp.PreparedStatementHandle.executeQuery(PreparedStatementHandle.java:179)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1869)
at org.hibernate.loader.Loader.doQuery(Loader.java:718)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
... 27 more
The Role and Permission classes have been mapped with JPA and some Hibernate annotations like this:
public abstract class Role implements Serializable {
/**
* The id of this role. Internal use only.
*
* #since 1.0
*/
#Id #GeneratedValue
protected long id;
/**
* Set of permissions granted to this role.
*
* #since 1.0
*/
#OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy="sourceRole")
protected Set<Permission> permissions = new HashSet<Permission>();
...
}
public class Permission implements Serializable {
private static final long serialVersionUID = 1L;
/**
* The id of this permission. Used internally for persistence.
*
* #since 1.0
*/
#Id #GeneratedValue
#Column(name = "PERMISSION_ID")
protected long id;
/**
* The group to which the owner of this permission is being granted permission to.
*
* #since 1.0
*/
#ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
#JoinColumn(name = "TARGET_ROLE_ID")
#ForeignKey(name = "FK_TARGET_GROUP_PERMISSION_ID",
inverseName = "FK_PERMISSION_ID_TARGET_GROUP")
protected Group targetGroup;
/**
* The role that has been granted this permission.
*
* #since 1.0
*/
#ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
#JoinColumn(name = "SOURCE_ROLE_ID")
#ForeignKey(name = "FK_SOURCE_GROUP", inverseName = "FK_GROUP_PERMISSIONS")
private Role sourceRole;
...
}
What is wrong with this criteria query?
I was expecting the call to typedQuery.getResultList() to return a list of collections with just one element: the collection of permission objects for the role with id = 2. This is an attempt to select (and initialize) just the "permissions" collection from the object role with id = 2.
I'm new to criteria queries and I'm having a hard time finding what's wrong with it.
Follow up:
Here is the query that was Hibernate is trying to execute:
select permission1_.PERMISSION_ID as PERMISSION1_12_,
permission1_.IS_REQUIRED as IS2_12_,
permission1_.SOURCE_ROLE_ID as SOURCE3_12_,
permission1_.TARGET_ROLE_ID as TARGET4_12_
from (
select ROLE_ID,
NAME,
DESCRIPTION,
IS_ACTION,
LABEL,
null as FIRST_NAME,
null as LAST_NAME,
null as PASSWORD_HASH,
1 as clazz_ from GROUPS
union
select ROLE_ID,
NAME,
null as DESCRIPTION,
null as IS_ACTION,
null as LABEL,
FIRST_NAME,
LAST_NAME,
PASSWORD_HASH,
2 as clazz_ from USERS
)
role0_ inner join PERMISSIONS permission1_ on role0_.ROLE_ID=permission1_.SOURCE_ROLE_ID
where (role0_.ROLE_ID=2L )
Clearly the problem is with the 2L. The 2L is really a string or a column from MySQL's perspective since it has that L on it and is not quoted.
Is this a bug in Hibernate? It looks like one to me. That L should not be in the generated SQL Query...
Anyone has any ideas of what that is happening? It seems to me that this would be a major bug that couldn't pass unnoticed by everyone else, so I'm assuming there is a good change I'm doing something wrong.
Thank you!!
Eduardo
you can try using alias for the two tables that you have used to create the join in criteria. This can also be the problem as hibernate only creates alias for HQL,s and entity loads but for criteria is rely on user to create the same.
The problem was solved by replacing the id attribute data type from long to Long. That simple change made Hibernate start placing the 2 without the L in the query, and no more failures occurred.