[Hibernate-JIRA] Created: (HHH-2796) Generated version are incremented by Hibernate
by Heba Tawfik (JIRA)
Generated version are incremented by Hibernate
----------------------------------------------
Key: HHH-2796
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2796
Project: Hibernate3
Issue Type: Bug
Components: core
Affects Versions: 3.2.1
Environment: 3.2.1, Oracle 10g
Reporter: Heba Tawfik
Creating a new entity which it's version is set to generated="always" & saving it, then in the same session adding an object to any of its on-to-many relationships is throwing a StaleObjectStateException.
To regenerate the problem, consider the following code
1- departement.hbm.xml :
----------------------------------------
<hibernate-mapping package="com.myproject.domain">
<class name="Departement" table="Departement">
<id name="id">
<column name="dept_id" />
</id>
<version column="version" generated="always" name="version" type="integer" unsaved-value="null" />
<property name="name" column="name" />
<bag name="employees" inverse="true" cascade="all" lazy="true">
<key column="dept_id"></key>
<one-to-many class="Employee" />
</bag>
</class>
</hibernate-mapping>
2- employee.hbm.xml :
--------------------------------------
<hibernate-mapping package="com.myproject.domain">
<class name="Employee" table="Employee">
<id name="id">
<column name="employee_id" />
</id>
<property name="firstName" column="first_name" />
<property name="lastName" column="last_name" />
<property name="age" column="age" />
<property name="salary" column="salary" />
<many-to-one cascade="none"
class="com.myproject.domain.Departement"
column="dept_id" embed-xml="true" insert="false"
name="department" not-null="false"
unique="false" update="false" not-found="ignore">
</many-to-one>
</class>
</hibernate-mapping>
3- Departement.Java
---------------------------
package com.myproject.domain;
import java.util.ArrayList;
import java.util.List;
public class Departement extends BaseDepartement{
int id;
String name;
List employees;
Integer version;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public List getEmployees() {
return employees;
}
public void setEmployees(List employees) {
this.employees = employees;
}
public void addToEmployeeList(Employee emp)
{
if(employees==null)
{
setEmployees(new ArrayList());
}
employees.add(emp);
}
}
4- Employee.Java :
---------------------------
package com.myproject.domain;
public class Employee {
int id;
String firstName;
String lastName;
int age;
int salary;
BaseDepartement department;
public BaseDepartement getDepartment() {
return department;
}
public void setDepartment(BaseDepartement department) {
this.department = department;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
4- Code To Test :
------------------------
public static void main(String args[]) throws Exception
{
SessionFactory factory = new Configuration().configure("com/myproject/config/hibernate.cfg.xml").buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Departement aDept =new Departement();
aDept.setId(10);
aDept.setName("Test");
session.save(aDept);
transaction.commit();
transaction = session.beginTransaction();
Employee emp=new Employee();
emp.setId(10);
emp.setFirstName("test");
emp.setLastName("test");
emp.setSalary(10);
emp.setAge(10);
emp.setDepartment(aDept);
aDept.addToEmployeeList(emp);
session.saveOrUpdate(aDept);
transaction.commit();
session.close();
}
On the database, the default value for version cloumn is set to "1" and a trigger is defined as follow on the Departement table:
create or replace
TRIGGER TRIGGER1
BEFORE UPDATE ON DEPARTEMENT
FOR EACH ROW
BEGIN
:new.version:= :old.version + 1;
END;
Upon executing the above code, the following is the hibernate logging :
automatically flushing session
/* insert com.eds.myproject.domain.Departement
*/ insert
into
Departement
(name, dept_id)
values
(?, ?)
binding 'Test' to parameter: 1
binding '10' to parameter: 2
/* get generated state com.eds.myproject.domain.Departement */ select
departemen_.version as version1_
from
Departement departemen_
where
departemen_.dept_id=?
binding '10' to parameter: 1
returning '1' as column: version1_
before transaction completion
after transaction completion
automatically flushing session
/* get current state com.eds.myproject.domain.Employee */ select
employee_.employee_id,
employee_.first_name as first2_0_,
employee_.last_name as last3_0_,
employee_.age as age0_,
employee_.salary as salary0_
from
Employee employee_
where
employee_.employee_id=?
binding '10' to parameter: 1
/* insert com.eds.myproject.domain.Employee
*/ insert
into
Employee
(first_name, last_name, age, salary, employee_id)
values
(?, ?, ?, ?, ?)
binding 'test' to parameter: 1
binding 'test' to parameter: 2
binding '10' to parameter: 3
binding '10' to parameter: 4
binding '10' to parameter: 5
/* update
com.eds.myproject.domain.Departement */ update
Departement
set
name=?
where
dept_id=?
and version=?
binding 'Test' to parameter: 1
binding '10' to parameter: 2
binding '2' to parameter: 3
Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.eds.myproject.domain.Departement#10]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1714)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2357)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2257)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2557)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.eds.myproject.test.Test.main(Test.java:43)
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.eds.myproject.domain.Departement#10]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1714)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2357)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2257)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2557)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.eds.myproject.test.Test.main(Test.java:43)
--
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
12 years
[Hibernate-JIRA] Created: (HHH-2776) Restrictons.in(..) generates invalid SQL if list of values is empty
by Adrian Smith (JIRA)
Restrictons.in(..) generates invalid SQL if list of values is empty
-------------------------------------------------------------------
Key: HHH-2776
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2776
Project: Hibernate3
Issue Type: Bug
Components: query-criteria
Affects Versions: 3.2.2
Environment: Hibernate 3.2.2.ga, MySQL 4.1.18, Windows XP
Reporter: Adrian Smith
I have classes called A and B. There is a 1:n relationship from A to B.
Class B has the following in its mapping file:
<many-to-one name="a" class="A" column="fk_a_id" not-null="true" />
If I write the following code:
List<B> foo() {
List<A> aList = ....
Session session = ....
Criteria query = session.createCriteria(B.class);
query.add(Restrictions.in("a", aList));
return query.list();
}
Then all works fine and the results I want are returned. However, if aList is empty (i.e. aList.size()==0) then the query.list function generates and executes invalid SQL for MySQL.
java.sql.SQLException: Syntax error or access violation message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1"
I imagine that it is generating SQL like IN () which isn't valid.
I realize it's a bit ridiculous to want to find the rows where a field is IN the empty list. Nothing will ever be returned. However, in my opinion it should still be allowed, and return no rows, just as saying "WHERE 1=2" is allowed, just doesn't return any rows.
--
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
12 years
[Hibernate-JIRA] Created: (HHH-2831) Native SQL queries with addJoin or <return-join/> return object arrays instead of single Entities
by Jeremy Grodberg (JIRA)
Native SQL queries with addJoin or <return-join/> return object arrays instead of single Entities
-------------------------------------------------------------------------------------------------
Key: HHH-2831
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2831
Project: Hibernate3
Issue Type: Bug
Components: query-sql
Affects Versions: 3.2.5, 3.2.4
Environment: Hibernate 3.2.4.ga and 3.2.5.ga with hsqldb on Win XP
Reporter: Jeremy Grodberg
Attachments: NativeSQLQueriesTest.patch
Although the documentation is not crystal clear, I read it to say that using addJoin should eagerly fetch an associated object but should NOT return it as a separate value. In section 16.1.3. "Handling associations and collections" of the online documentation it says: "It is possible to eagerly join in the Dog to avoid the possible extra roundtrip for initializing the proxy. This is done via the addJoin() method, which allows you to join in an association or collection." This comes BEFORE the section on returning multiple entities, so I say the documentation at least implies it will only return a single entity at the top level. Also, if the intention is to return multiple entities, addEntity() works fine for that, so what then would be the difference of addJoin()? If I'm wrong about what addJoin() should do, please clarify that in the documentation and also clarify how, if it is possible, one could eagerly fetch the association without changing the return type of the quer
y.
I have reproduced this problem in the Hibernate JUnit tests in 3.2.4.ga and 3.2.5.ga, specifically NativeSQLQueriesTest.testSQLQueryInterface().
I'm attaching a patch to the Hibernate junit test org.hibernate.test.sql.hand.query.NativeSQLQueriesTest.java released in 3.2.5.ga that adds a test to ensure that a query with addJoin only returns a (list of) entities, not a list of Object arrays containing entities. Currently, the assertion fails because instead of returning a list of Organizations we get a list of Object[3] = { Organization, Employment, Person }, which is exactly what we get when the addJoin()s are replaced with appropriate addEntity() calls.
--
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
12 years
[Hibernate-JIRA] Created: (ANN-654) Generated SQL includes a column named "null" when referencing a map entry by key and using @LazyCollection(LazyCollectionOption.EXTRA)
by Paul Roe (JIRA)
Generated SQL includes a column named "null" when referencing a map entry by key and using @LazyCollection(LazyCollectionOption.EXTRA)
--------------------------------------------------------------------------------------------------------------------------------------
Key: ANN-654
URL: http://opensource.atlassian.com/projects/hibernate/browse/ANN-654
Project: Hibernate Annotations
Issue Type: Bug
Components: binder
Affects Versions: 3.2.0.ga
Environment: Oracle 10g, hibernate 3.2
Reporter: Paul Roe
We have an Product entity with a map of RegionProduct by Region.
@MapKey(name="region")
@OneToMany(mappedBy="product", targetEntity= RegionProductImpl.class, fetch = FetchType.LAZY)
@LazyCollection(LazyCollectionOption.EXTRA)
private Map<Region, RegionProduct> regionProducts = new HashMap<Region, RegionProduct>();
When accessing an element of the map by key, the SQL that is generated includes a "null" column in the WHERE condition.
where regionprod0_.PROD_ID=? and regionprod0_.null=?
resulting in the stack trace
org.hibernate.exception.SQLGrammarException: could not collection element by index
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1841)
at org.hibernate.loader.entity.CollectionElementLoader.loadElement(CollectionElementLoader.java:72)
at org.hibernate.persister.collection.OneToManyPersister.getElementByIndex(OneToManyPersister.java:360)
at org.hibernate.collection.AbstractPersistentCollection.readElementByIndex(AbstractPersistentCollection.java:158)
at org.hibernate.collection.PersistentMap.get(PersistentMap.java:146)
Iterating over the collection works successfully, but populates the entire collection, which is what Extra-Lazy is meant to avoid.
--
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
12 years, 1 month
[Hibernate-JIRA] Created: (HHH-2891) Query cache fails on many-to-many select
by Oleg Efimov (JIRA)
Query cache fails on many-to-many select
----------------------------------------
Key: HHH-2891
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2891
Project: Hibernate3
Issue Type: Bug
Components: query-hql
Affects Versions: 3.2.5
Reporter: Oleg Efimov
I have two entities, User and Group, with many-to-many relation:
-------------------------------------------------------------------------------------------------- User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.hibernate">
<class name="User" table="`USER`">
<id name="id" type="long" unsaved-value="null">
<column name="USER_ID" not-null="true"/>
<generator class="native"/>
</id>
<property name="login">
<column name="LOGIN" not-null="true" length="255"/>
</property>
<bag name="groups" table="USER_GROUP" lazy="false" cascade="none">
<key column="USER_ID"/>
<many-to-many class="Group" column="GROUP_ID"/>
</bag>
</class>
</hibernate-mapping>
-------------------------------------------------------------------------------------------------- Group.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.hibernate">
<class name="Group" table="`GROUP`">
<id name="id" type="long" unsaved-value="null">
<column name="GROUP_ID" not-null="true"/>
<generator class="native"/>
</id>
<property name="name">
<column name="GROUPNAME" not-null="true" length="100"/>
</property>
<bag name="users" table="USER_GROUP" lazy="true" inverse="true" cascade="all-delete-orphan">
<key column="GROUP_ID"/>
<many-to-many class="User" column="USER_ID"/>
</bag>
</class>
<query name="Group.select.by.login" cacheable="true"><![CDATA[
select user.groups from User user where user.login=?
]]>
</query>
</hibernate-mapping>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test.hibernate.User and test.hibernate.Group classes are just simple beans.
In Group.hbm.xml there is a query called "Group.select.by.login", it's intended to be cached. So I add corresponding properties to Hibernate configuraition:
--------------------------------------------------------------------------------------------------
properties.put("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
properties.put("hibernate.cache.use_query_cache", "true");
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Configuration has no extra parameters.
Then I'm trying to execute the query two times:
-------------------------------------------------------------------------------------------------- test fragment
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = null;
try {
session = sessionFactory.openSession();
Query query = session.getNamedQuery("Group.select.by.login");
List<Group> groups;
groups = query.setParameter(0, "admin").list(); // +++++++++++++++This list() returns list of two Group objects, ok +++++++++++++++
System.out.println("-----------------------------------------------------------------------------------");
for (Group group : groups) {
System.out.println(group.getName());
}
System.out.println("-----------------------------------------------------------------------------------");
groups = query.setParameter(0, "admin").list(); // +++++++++++++++This list() returns list of two nulls +++++++++++++++
System.out.println("-----------------------------------------------------------------------------------");
for (Group group : groups) {
System.out.println(group.getName());
}
System.out.println("-----------------------------------------------------------------------------------");
} finally {
if (session != null) {
session.close();
}
sessionFactory.close();
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The first query returns list of, in my case, two groups. The second query returns list of two nulls instead of the same list.
I've investigated the problem, and found the following:
Query return value is "user.groups", and during query analysis (org.hibernate.hql.ast.QueryTranslatorImpl:160) query return type is defined as "org.hibernate.type.BagType(test.hibernate.User.groups)". This seems to be the problem, as during query put operation org.hibernate.CollectionType.disassemble is called (org.hibernate.cache.StandardQueryCache:80), which simply returns null as owner is null.
If this query is not a misuse, then I think, something is to be done at the time of analysis, so that return type be somewhat different.
Thanks.
--
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
12 years, 1 month
[Hibernate-JIRA] Created: (HHH-2223) BatchFetchQueue.getXXXBatch() cache check impeding batch fetching
by Joel Caplin (JIRA)
BatchFetchQueue.getXXXBatch() cache check impeding batch fetching
-----------------------------------------------------------------
Key: HHH-2223
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2223
Project: Hibernate3
Type: Bug
Components: core
Versions: 3.2.0.ga
Environment: 3.2.0GA, Mac OS X/Solaris 10, Sybase 12.5/HSQLDB
Reporter: Joel Caplin
Priority: Minor
Attachments: testcase.tar
I have attached a test case which demonstrates the issue at hand by implementing a cheap data model: Pub >---|- Manager -|--< Employee
Pub and Employee are uncached; Manager is cached (tried under both Eh and the non-prod HashMap implementation). All associations are lazy. The test case has a default batch size of 10 and uses hsqldb to demonstrate the problem - which was diagnosed against Sybase.
When BatchFetchQueue.getXXXBatch() is invoked, it does not load any objects it finds in the second level cache - it merely acknowledges their presence by logging to INFO (isCached()). This in turn means that any child associations in that cached object will not have a proxy created for them.
Suppose we want to find all the employees of the pubs in our database. We choose to do this by loading all the Pubs ("FROM Pub") and calling thePub.getManager().getEmployees() iteratively.
When the managers are in the second level cache, Hibernate issues 11 select statements: one to load the pubs and one for each distinct manager.
When the Managers are not in the cache, Hibernate issues 3 select statements: one to load the pubs, one to load the managers and one to load the employees.
A solution might be to load the object from the cache instead of just ignoring it?
--
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
12 years, 2 months
[Hibernate-JIRA] Created: (HHH-2280) Not-Null Constraint-Violation with unidirectional one-to-many mapping
by Robert Herschke (JIRA)
Not-Null Constraint-Violation with unidirectional one-to-many mapping
---------------------------------------------------------------------
Key: HHH-2280
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2280
Project: Hibernate3
Type: Bug
Versions: 3.2.0.cr4
Environment: Hibernate 3.2.0.cr4, Oracle 10XE
Reporter: Robert Herschke
Priority: Blocker
Attachments: one-to-many-test.zip
In some cases it is neccessary to have an unidirectional one-to-many mapping, but not using Join-Tables due to a legacy database system.
So it must be possible, to have a one-to-many mapping just via a foreign-key-mapping. The Child-Object must not know the Parent-Object in this case.
It must also be possible, to pay respect to a NOT-NULL-Constraint at this foreign-key due to restrictions on legacy database systems.
Hibernate doesn't support this, but throw a NOT-NULL-Constraint Violation Exception. The documentation says, "this is unusual" - to which I want to dissent.
See the attached testcase for details, that is adopted from your "one-to-many" testcase found in the 3.2.0 sources.
--
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
12 years, 2 months