[Hibernate-JIRA] Created: (HHH-3771) Best pactice for equals implementation?
by Samppa Saarela (JIRA)
Best pactice for equals implementation?
---------------------------------------
Key: HHH-3771
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3771
Project: Hibernate Core
Issue Type: New Feature
Components: core, documentation
Reporter: Samppa Saarela
When domain model contains even one lazy reference to an object, default equals fails when it's compared to a) the actual implementation returned by Session.get/load or b) other proxies (at least of different supertype). Overriding equals on a class that uses surrogate id is not that simple. However there is a simple solution to this problem:
In domain class, override equals like this:
public boolean equals(Object obj) {
return this == getImplementation(obj);
}
public static Object getImplementation(Object obj) {
if (obj instanceof HibernateProxy) {
return ((HibernateProxy) obj).getHibernateLazyInitializer().getImplementation();
} else {
return obj;
}
}
This should result always in comparing object references of actual instances and thus preserve symmetry.
It's understandable that you don't wan to publish that kind of getImplementation utility e.g. in Hibernate, but maybe you could support this more directly by implementing
Hibernate.equals(Object o1, Object o2)
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 2 months
[Hibernate-JIRA] Created: (HHH-5097) Bug in ParameterizedFunctionExpression with two or more parameters: IllegalArgumentException
by Max Hartmann (JIRA)
Bug in ParameterizedFunctionExpression with two or more parameters: IllegalArgumentException
--------------------------------------------------------------------------------------------
Key: HHH-5097
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5097
Project: Hibernate Core
Issue Type: Bug
Components: entity-manager
Affects Versions: 3.5.0-Final
Environment: oracle 10g, hibernate 3.5
Reporter: Max Hartmann
If you call CriteriaBuilder.function() respectively "ParameterizedFunctionExpression" with two or more arguments you get an IllegalArgumentException.
I think the bug is here:
protected void renderArguments(StringBuilder buffer, CriteriaQueryCompiler.RenderingContext renderingContext) {
for ( Expression argument : argumentExpressions ) {
buffer.append( ( (Renderable) argument ).render( renderingContext ) );
}
}
The arguments are not seperated by comma.
Exception:
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: : near line 1, column 37 [select TO_CHAR(generatedAlias0.date1:param0) from test.server.TestBean as generatedAlias0]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1166)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1112)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:315)
at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:154)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:432)
at test.server.TestApp.main(TestApp.java:28)
Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: : near line 1, column 37 [select TO_CHAR(generatedAlias0.date1:param0) from test.server.TestBean as generatedAlias0]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:297)
... 3 more
Code:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("userDatabase");
EntityManager em = factory.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> createQuery = cb.createQuery(String.class);
Root<TestBean> from = createQuery.from(TestBean.class);
Expression<String> param = cb.literal("DD.MM.YYYY");
createQuery.select(cb.function("TO_CHAR", String.class,
from.get(TestBean_.date1), param));
List<String> resultList = em.createQuery(createQuery).getResultList();
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 2 months
[Hibernate-JIRA] Created: (HHH-3908) Expose way to fully control fetching in native-sql queries in API
by Steve Ebersole (JIRA)
Expose way to fully control fetching in native-sql queries in API
-----------------------------------------------------------------
Key: HHH-3908
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3908
Project: Hibernate Core
Issue Type: New Feature
Components: core
Reporter: Steve Ebersole
Fix For: 3.5
Currently, in order to fully control fetching in native-sql queries, users must revert to using a named sql query (at least the xml, not sure if the annotations variety supports as well). We alreasy have all the objects/contracts in place to handle this for the named queries, just need to clean-up and properly document them.
The current API calls to deal with this are the overloaded SQLQuery#addJoin methods. Ideally I'd see these changed to return the representation of the join-fetch to be configured; but addJoin already defines a return : the query itself :( So probably we will need new methods like addFetch:
1) public JoinFetch addFetch(String alias, String ownerAlias, String ownerProperty)
2) public JoinFetch addFetch(String alias, String ownerAlias, String ownerProperty, LockMode lockMode)
interface JoinFetch {
public void addPropertyMapping(String propertyName, String sqlAlias);
}
This can be expanded to the "root returns" as well (currently the overloaded #addEntity methods):
public RootReturn addRoot(String alias, Class entityClass)
etc...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQLQuery query = session.createSQLQuery(
"select c.cust_id as cid, " +
" c.cust_name as cname, " +
" o.order_id as oid, " +
" o.order_num as onum " +
" from customer c " +
" inner join orders o " +
" on c.cust_id = o.cust_id"
);
query.addRoot( "c", Customer.class )
.addPropertyMapping( "id", "cid" )
.addPropertyMapping( "name", "cname" );
query.addFetch( "o", "c", "orders" )
.addPropertyMapping( "id", "oid" )
.addPropertyMapping( "orderNumber", "onum" );
...
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 2 months
[Hibernate-JIRA] Created: (HHH-3609) Invalid list re-ordering with IndexColumn and OneToMany association
by Martin Kovacik (JIRA)
Invalid list re-ordering with IndexColumn and OneToMany association
-------------------------------------------------------------------
Key: HHH-3609
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3609
Project: Hibernate Core
Issue Type: Bug
Components: core
Affects Versions: 3.3.1
Environment: Hibernate 3.3.1, Hibernate Tools 3.2.0, H2 database 1.0.69
Reporter: Martin Kovacik
I want to map unidirectional OneToMany ordered list using annotations. My mapping is:
public class Invoice {
// ...
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "INVOICE_ITEM",
joinColumns = {@JoinColumn(name = "invoice_id")},
inverseJoinColumns = {@JoinColumn(name = "item_id")}
)
@Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@IndexColumn(name = "item_order")
private List<Item> items = new ArrayList<Item>();
}
The mapping of Item is not important here because I just want the association from Invoice to collection of Items to be unidirectional.
The problem what hbm2ddl generates:
create table INVOICE_ITEM (
invoice_id int8 not null,
item_id int8 not null,
item_order int4 not null,
primary key (invoice_id, item_order),
unique (item_id)
);
alter table INVOICE_ITEM
add constraint FK89837AD3EDA329D
foreign key (item_id)
references ITEM;
alter table INVOICE_ITEM
add constraint FK89837AD76A8E2D7
foreign key (invoice_id)
references INVOICE;
Another problem is how hibernate handles list re-ordering. If I want to change the item order (by manipulating invoice.items list) and then persist the changes hibernate tries to execute following sql:
update
INVOICE_ITEM
set
item_id=2
where
invoice_id=1
and item_order=0
update
INVOICE_ITEM
set
item_id=1
where
invoice_id=1
and item_order=1
However this causes DB to complain about unique item_id constraint (which was generated by hibernate). IMHO hibernate should update just the item_order column.
I think that this bug is related to ANN-679 (http://opensource.atlassian.com/projects/hibernate/browse/ANN-679).
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
14 years, 2 months