[
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2842?page=c...
]
Diego Pires Plentz updated HHH-2842:
------------------------------------
Description:
Hi,
I am working on a project migrating a legacy application into J2EE environment using
Hibernate.
Although it has fairly complicated class hierarchy and table structures, we have managed
to migrate most of features successfully, thanks to the flexibility of Hibernate. (Thank
you for the great work!)
However, we have one issue with instantiating dependent objects.
Sometimes only containing class knows how to instantiate its dependent objects properly,
so these objects need to be created by the containing class.
For example, the following persistent class Student has a dependent object studentInfo,
which is mapped as as component in the mapping file.
--------------------------------------------------
public class Student {
private Long id;
private StudentInfo studentInfo;
public Student() {
// instantiate StudentInfo object here with some additional information.
studentInfo = new StudentInfo(SchoolHelper.lookupType("C"));
}
// ...
}
--------------------------------------------------
public class StudentInfo {
private String description;
private transient String typeA;
private transient String typeB;
public StudentInfo() {
}
public StudentInfo(String type) {
this.typeA = SchoolHelper.getTypeA(type);
this.typeB = SchoolHelper.getTypeB(type);
}
// ...
--------------------------------------------------
<class name="school.Student" table="STUDENT">
<id name="id" column="STUDENT_ID">
<generator class="native"/>
</id>
<component name="studentInfo"
class="school.StudentInfo">
<property name="description" column="SI_DESC" />
</component>
</class>
--------------------------------------------------
Only 'description' member is mapped to a database column and other transient
members of StudentInfo aren't. These transient members can be set only by using the
information passed from its parent class, Student.
However, when a Student object is loaded from database, this information (typeA and typeB)
becomes null, because the default constructor StudentInfo() is used by Hibernate, and then
Student.setStudentInfo() is called to replace the existing (but properly instantiated)
studentInfo object.
I know it is a Hibernate requirement for all persistent classes to have no-arg default
constructors.
This is fine, but is it true for all dependent objects as well?
Some of our depedent objects are contained in both persistent classes and business logic
classes and it is very difficult to separate them.
It would be great if Hibernate supports either of following features:
1) <property> can accept a dotted notaton for its name, so that we can access
directly the member of the depedent object without having <component>.
i.e.
<property name="studentInfo.description" column="SI_DESC"
/>
<!-- this will call getStudentInfo().setDescription(). -->
or,
2) <component> has an option to indicate to reuse the existing instance instead of
always creating a new one.
i.e.
<component name="studentInfo" reuse="true"
class="school.StudentInfo>
...
</component>
<!-- the existing studentInfo object will be used (unless it is null), instead of
creating one with its default constructor -->
I have attached student.zip to indicate this issue and quickfix.zip for the (rough)
hibernate fix so that ComponentType reuses the existing value unless it is null.
Any advice or comment would be appreciated.
was:
Hi,
I am working on a project migrating a legacy application into J2EE environment using
Hibernate.
Although it has fairly complicated class hierarchy and table structures, we have managed
to migrate most of features successfully, thanks to the flexibility of Hibernate. (Thank
you for the great work!)
However, we have one issue with instantiating dependent objects.
Sometimes only parent class knows how to instantiate its dependent objects properly, so
these objects need to be created by their parent class.
For example, the following persistent class Student has a dependent object studentInfo,
which is mapped as as component in the mapping file.
--------------------------------------------------
public class Student {
private Long id;
private StudentInfo studentInfo;
public Student() {
// instantiate StudentInfo object here with some additional information.
studentInfo = new StudentInfo(SchoolHelper.lookupType("C"));
}
// ...
}
--------------------------------------------------
public class StudentInfo {
private String description;
private transient String typeA;
private transient String typeB;
public StudentInfo() {
}
public StudentInfo(String type) {
this.typeA = SchoolHelper.getTypeA(type);
this.typeB = SchoolHelper.getTypeB(type);
}
// ...
--------------------------------------------------
<class name="school.Student" table="STUDENT">
<id name="id" column="STUDENT_ID">
<generator class="native"/>
</id>
<component name="studentInfo"
class="school.StudentInfo">
<property name="description" column="SI_DESC" />
</component>
</class>
--------------------------------------------------
Only 'description' member is mapped to a database column and other transient
members of StudentInfo aren't. These transient members can be set only by using the
information passed from its parent class, Student.
However, when a Student object is loaded from database, this information (typeA and typeB)
becomes null, because the default constructor StudentInfo() is used by Hibernate, and then
Student.setStudentInfo() is called to replace the existing (but properly instantiated)
studentInfo object.
I know it is a Hibernate requirement for all persistent classes to have no-arg default
constructors.
This is fine, but is it true for all dependent objects as well?
Some of our depedent objects are contained in both persistent classes and business logic
classes and it is very difficult to separate them.
It would be great if Hibernate supports either of following features:
1) <property> can accept a dotted notaton for its name, so that we can access
directly the member of the depedent object without having <component>.
i.e.
<property name="studentInfo.description" column="SI_DESC"
/>
<!-- this will call getStudentInfo().setDescription(). -->
or,
2) <component> has an option to indicate to reuse the existing instance instead of
always creating a new one.
i.e.
<component name="studentInfo" reuse="true"
class="school.StudentInfo>
...
</component>
<!-- the existing studentInfo object will be used (unless it is null), instead of
creating one with its default constructor -->
I have attached student.zip to indicate this issue and quickfix.zip for the (rough)
hibernate fix so that ComponentType reuses the existing value unless it is null.
Any advice or comment would be appreciated.
Problem with instantiating components
-------------------------------------
Key: HHH-2842
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2842
Project: Hibernate3
Issue Type: Improvement
Components: core
Affects Versions: 3.2.5
Environment: Hibernate 3.2.5ga, DB2 8
Reporter: Lanna Kim
Attachments: quickfix.zip, school.zip
Hi,
I am working on a project migrating a legacy application into J2EE environment using
Hibernate.
Although it has fairly complicated class hierarchy and table structures, we have managed
to migrate most of features successfully, thanks to the flexibility of Hibernate. (Thank
you for the great work!)
However, we have one issue with instantiating dependent objects.
Sometimes only containing class knows how to instantiate its dependent objects properly,
so these objects need to be created by the containing class.
For example, the following persistent class Student has a dependent object studentInfo,
which is mapped as as component in the mapping file.
--------------------------------------------------
public class Student {
private Long id;
private StudentInfo studentInfo;
public Student() {
// instantiate StudentInfo object here with some additional information.
studentInfo = new StudentInfo(SchoolHelper.lookupType("C"));
}
// ...
}
--------------------------------------------------
public class StudentInfo {
private String description;
private transient String typeA;
private transient String typeB;
public StudentInfo() {
}
public StudentInfo(String type) {
this.typeA = SchoolHelper.getTypeA(type);
this.typeB = SchoolHelper.getTypeB(type);
}
// ...
--------------------------------------------------
<class name="school.Student" table="STUDENT">
<id name="id" column="STUDENT_ID">
<generator class="native"/>
</id>
<component name="studentInfo"
class="school.StudentInfo">
<property name="description" column="SI_DESC" />
</component>
</class>
--------------------------------------------------
Only 'description' member is mapped to a database column and other transient
members of StudentInfo aren't. These transient members can be set only by using the
information passed from its parent class, Student.
However, when a Student object is loaded from database, this information (typeA and
typeB) becomes null, because the default constructor StudentInfo() is used by Hibernate,
and then Student.setStudentInfo() is called to replace the existing (but properly
instantiated) studentInfo object.
I know it is a Hibernate requirement for all persistent classes to have no-arg default
constructors.
This is fine, but is it true for all dependent objects as well?
Some of our depedent objects are contained in both persistent classes and business logic
classes and it is very difficult to separate them.
It would be great if Hibernate supports either of following features:
1) <property> can accept a dotted notaton for its name, so that we can access
directly the member of the depedent object without having <component>.
i.e.
<property name="studentInfo.description" column="SI_DESC"
/>
<!-- this will call getStudentInfo().setDescription(). -->
or,
2) <component> has an option to indicate to reuse the existing instance instead
of always creating a new one.
i.e.
<component name="studentInfo" reuse="true"
class="school.StudentInfo>
...
</component>
<!-- the existing studentInfo object will be used (unless it is null), instead of
creating one with its default constructor -->
I have attached student.zip to indicate this issue and quickfix.zip for the (rough)
hibernate fix so that ComponentType reuses the existing value unless it is null.
Any advice or comment would be appreciated.
--
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