Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [Teneo] Generated Many to Many not populating resolving table
[Teneo] Generated Many to Many not populating resolving table [message #424364] Fri, 24 October 2008 15:53 Go to next message
David Wynter is currently offline David WynterFriend
Messages: 4624
Registered: July 2009
Senior Member
Hi,

I have these two entities:

<xsd:complexType name="User">
<xsd:annotation>
<xsd:documentation>
<xsd:p>The basic user entity. Role is a one to many, many to
one association</xsd:p>
</xsd:documentation>
</xsd:annotation>
<xsd:sequence minOccurs="0" maxOccurs="1">
</xsd:element>
<xsd:element minOccurs="0" maxOccurs="unbounded" name="userRole"
type="secmsec:Role">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa"> @OneToMany(indexed=false,
mappedBy="role") @JoinTable(name="UserRole")</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:int">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa">@Id
@GeneratedValue(strategy=IDENTITY)</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="firstName" type="xsd:string" />
</xsd:complexType>
<xsd:element name="user" type="secmsec:User">
</xsd:element>


<xsd:complexType name="Role">
<xsd:sequence minOccurs="0" maxOccurs="1">
<xsd:element minOccurs="0" maxOccurs="unbounded"
name="permission" type="secmsec:Permission" ecore:opposite="role"/>
<xsd:element minOccurs="0" maxOccurs="unbounded" name="roleUser"
type="secmsec:User">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa"> @OneToMany(indexed=false,
mappedBy="user") @JoinTable(name="UserRole")</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:int">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa">@Id</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="name" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa">@Column(unique=true)</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>

I want to manually control the ids for Role, thus not @GeneratedValue.

This generates the required userrole table without problem. However when
I try to use Hibernate to populate this table never gets populated. Not
sure if it is my use of Hibernate or if I am missing a annotation.

I am using this code

// We will insert records for each of the cases below
Session sess = getCurrentSession();
Transaction tx = sess.beginTransaction();

try
{
// add the standard Role objects
Role adminRole = secmsecFactory.eINSTANCE.createRole();
adminRole.setId(1);
adminRole.setName("admin");
sess.save(adminRole);

// add the standard User for admin
User adminUser = secmsecFactory.eINSTANCE.createUser();
adminUser.setFirstName("Super");
adminUser.setId(1);
adminUser.getUserRole().add(adminRole);
sess.save(adminUser);

tx.commit();
....


Thx.

David
Re: [Teneo] Generated Many to Many not populating resolving table [message #424440 is a reply to message #424364] Mon, 27 October 2008 09:07 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030605010608040600010302
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi David,
I tested it and the main reason why this does not work is that you have specified mapped-by on both
sides. Setting mapped-by on an association means that the association is handled from the other
side. Let me explain: say you have an association between types A and B, the efeature on the A side
is called toB and on the B side toA. Then if you have a onetomany annotation with mappedby on toB
then when persisting A the association to B is not stored because hibernate assumes that B (the
other side) manages the relation. This can be solved by setting cascade options on toB also, which
will result in B also being persisted (with the relation to A).

Using mappedBy makes the most sense if you want to control where the joincolumn is placed in a
association using foreign keys, so with a jointable you don't need it. Also the mappedBy you
specified had the wrong values afaics it should have been roleUser instead of user and userRole
instead of role.

Some other things which I would change:
- I would not use containment between user and role, afaics these are just standard references
between 2 types. In emf references/associations are modeled in xsd using anyuri and ecore:reference
- You did not specify ecore:opposite therefore Teneo/EMF can not see that this is a bidirectional
relation
- You used a OneToMany while you mention that it is a ManyToMany relation

I have attached the xsd I have used to get it working correctly, the xsd contains the changes above.

Teneo will now throw an exception if you have set mappedby on both sides of the relation as this can
lead to difficult to understand issues.

gr. Martin

David Wynter wrote:
> Hi,
>
> I have these two entities:
>
> <xsd:complexType name="User">
> <xsd:annotation>
> <xsd:documentation>
> <xsd:p>The basic user entity. Role is a one to many, many to one
> association</xsd:p>
> </xsd:documentation>
> </xsd:annotation>
> <xsd:sequence minOccurs="0" maxOccurs="1">
> </xsd:element>
> <xsd:element minOccurs="0" maxOccurs="unbounded"
> name="userRole" type="secmsec:Role">
> <xsd:annotation>
> <xsd:appinfo source="teneo.jpa"> @OneToMany(indexed=false,
> mappedBy="role") @JoinTable(name="UserRole")</xsd:appinfo>
> </xsd:annotation>
> </xsd:element>
> </xsd:sequence>
> <xsd:attribute name="id" type="xsd:int">
> <xsd:annotation>
> <xsd:appinfo source="teneo.jpa">@Id
> @GeneratedValue(strategy=IDENTITY)</xsd:appinfo>
> </xsd:annotation>
> </xsd:attribute>
> <xsd:attribute name="firstName" type="xsd:string" />
> </xsd:complexType>
> <xsd:element name="user" type="secmsec:User">
> </xsd:element>
>
>
> <xsd:complexType name="Role">
> <xsd:sequence minOccurs="0" maxOccurs="1">
> <xsd:element minOccurs="0" maxOccurs="unbounded"
> name="permission" type="secmsec:Permission"
> ecore:opposite="role"/>
> <xsd:element minOccurs="0" maxOccurs="unbounded" name="roleUser"
> type="secmsec:User">
> <xsd:annotation>
> <xsd:appinfo source="teneo.jpa"> @OneToMany(indexed=false,
> mappedBy="user") @JoinTable(name="UserRole")</xsd:appinfo>
> </xsd:annotation>
> </xsd:element>
> </xsd:sequence>
> <xsd:attribute name="id" type="xsd:int">
> <xsd:annotation>
> <xsd:appinfo source="teneo.jpa">@Id</xsd:appinfo>
> </xsd:annotation>
> </xsd:attribute>
> <xsd:attribute name="name" type="xsd:string">
> <xsd:annotation>
> <xsd:appinfo
> source="teneo.jpa">@Column(unique=true)</xsd:appinfo>
> </xsd:annotation>
> </xsd:attribute>
> </xsd:complexType>
>
> I want to manually control the ids for Role, thus not @GeneratedValue.
>
> This generates the required userrole table without problem. However when
> I try to use Hibernate to populate this table never gets populated. Not
> sure if it is my use of Hibernate or if I am missing a annotation.
>
> I am using this code
>
> // We will insert records for each of the cases below
> Session sess = getCurrentSession();
> Transaction tx = sess.beginTransaction();
>
> try
> {
> // add the standard Role objects
> Role adminRole = secmsecFactory.eINSTANCE.createRole();
> adminRole.setId(1);
> adminRole.setName("admin");
> sess.save(adminRole);
>
> // add the standard User for admin
> User adminUser = secmsecFactory.eINSTANCE.createUser();
> adminUser.setFirstName("Super");
> adminUser.setId(1);
> adminUser.getUserRole().add(adminRole);
> sess.save(adminUser);
>
> tx.commit();
> ....
>
>
> Thx.
>
> David


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org

--------------030605010608040600010302
Content-Type: text/xml;
name="role.xsd"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="role.xsd"

PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NPHhzZDpz Y2hlbWEgdGFy
Z2V0TmFtZXNwYWNlPSJodHRwOi8vd3d3LmVjbGlwc2Uub3JnL2VtZi90ZW5l by90ZXN0L3Jv
bGUiIHhtbG5zOmVjb3JlPSJodHRwOi8vd3d3LmVjbGlwc2Uub3JnL2VtZi8y MDAyL0Vjb3Jl
Ig0JeG1sbnM6dGhpcz0iaHR0cDovL3d3dy5lY2xpcHNlLm9yZy9lbWYvdGVu ZW8vdGVzdC9y
b2xlIiB4bWxuczp4c2Q9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2No ZW1hIj4NCTx4
c2Q6Y29tcGxleFR5cGUgbmFtZT0iVXNlciI+DQkJPHhzZDphbm5vdGF0aW9u Pg0JCQk8eHNk
OmRvY3VtZW50YXRpb24+DSAgIC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W5u
b3RhdGlvbj4NCQk8L3hzZDphdHRyaWJ1dGU+DQk8L3hzZDpjb21wbGV4VHlw ZT4NCjwveHNk
OnNjaGVtYT4=
--------------030605010608040600010302--
Re: [Teneo] Generated Many to Many not populating resolving table [message #424508 is a reply to message #424440] Tue, 28 October 2008 18:47 Go to previous messageGo to next message
David Wynter is currently offline David WynterFriend
Messages: 4624
Registered: July 2009
Senior Member
Hi,

Here is a new problem. I changed that really large xsd to use the
ManyToMany annotation. The element in question has 17 many to many
relationships in this large model. The XSD reloads to the genmodel
without error, then the Model source code generates without error.

But when I come to generate the database schema using the initialise
method I get this

Exception in thread "main"
org.eclipse.emf.teneo.annotations.parser.AnnotationParserExc eption: Only
typenames are allowed at the root of the annotation, see _ for the error
ManyToMany_(indexed=false) @JoinTable(name="HolderActionSubclassAgent")

here is the call stack

Thread [main] (Suspended (exception AnnotationParserException))
AnnotationParser.parse(ENamedElement, String) line: 49
HbEAnnotationParserImporter(EAnnotationParserImporter).proce ss(EAnnotation,
ENamedElement) line: 159
HbEAnnotationParserImporter(EAnnotationParserImporter).proce ssAnnotatedModelElement(PAnnotatedEModelElement,
EPackage) line: 93
HbEAnnotationParserImporter(EAnnotationParserImporter).proce ss(PAnnotatedEClass)
line: 83
HbEAnnotationParserImporter(EAnnotationParserImporter).proce ss(PAnnotatedEPackage)
line: 72
HbEAnnotationParserImporter(EAnnotationParserImporter).proce ss(PAnnotatedModel)
line: 64
PersistenceMappingBuilder.buildMapping(List<EPackage>,
PersistenceOptions, ExtensionManager) line: 110
PersistenceMappingBuilder.buildMapping(EPackage[], PersistenceOptions,
ExtensionManager) line: 67
HbSessionDataStore(HbDataStore).mapEPackages() line: 624
HbSessionDataStore.mapModel() line: 155
HbSessionDataStore.initialize() line: 73
HibernateUtil.init(boolean) line: 87
HibernateUtil.main(String[]) line: 115


But this element has identical structure to the others. Here are two of them

<xsd:element minOccurs="0" maxOccurs="unbounded"
ecore:opposite="distributionSubclassAgent"
name="agentDistributionSubclass" type="xsd:anyURI"
ecore:reference="gensec:DistributionSubclass">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa"> @ManyToMany(indexed=false)
@JoinTable(name="DistributionSubclassAgent")</xsd:appinfo >
</xsd:annotation>
</xsd:element>
<xsd:element minOccurs="0" maxOccurs="unbounded"
ecore:opposite="holderActionSubclassAgent"
name="agentHolderActionSubclass" type="xsd:anyURI"
ecore:reference="gensec:HolderActionSubclass">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa"> @ManyToMany(indexed=false)
@JoinTable(name="HolderActionSubclassAgent")</xsd:appinfo >
</xsd:annotation>
</xsd:element>

Not sure why it has a problem with this particular element.

David

Martin Taal wrote:
> Hi David,
> I tested it and the main reason why this does not work is that you have
> specified mapped-by on both sides. Setting mapped-by on an association
> means that the association is handled from the other side. Let me
> explain: say you have an association between types A and B, the efeature
> on the A side is called toB and on the B side toA. Then if you have a
> onetomany annotation with mappedby on toB then when persisting A the
> association to B is not stored because hibernate assumes that B (the
> other side) manages the relation. This can be solved by setting cascade
> options on toB also, which will result in B also being persisted (with
> the relation to A).
>
> Using mappedBy makes the most sense if you want to control where the
> joincolumn is placed in a association using foreign keys, so with a
> jointable you don't need it. Also the mappedBy you specified had the
> wrong values afaics it should have been roleUser instead of user and
> userRole instead of role.
>
> Some other things which I would change:
> - I would not use containment between user and role, afaics these are
> just standard references between 2 types. In emf references/associations
> are modeled in xsd using anyuri and ecore:reference
> - You did not specify ecore:opposite therefore Teneo/EMF can not see
> that this is a bidirectional relation
> - You used a OneToMany while you mention that it is a ManyToMany relation
>
> I have attached the xsd I have used to get it working correctly, the xsd
> contains the changes above.
>
> Teneo will now throw an exception if you have set mappedby on both sides
> of the relation as this can lead to difficult to understand issues.
>
> gr. Martin
>
> David Wynter wrote:
>> Hi,
>>
>> I have these two entities:
>>
>> <xsd:complexType name="User">
>> <xsd:annotation>
>> <xsd:documentation>
>> <xsd:p>The basic user entity. Role is a one to many, many to
>> one association</xsd:p>
>> </xsd:documentation>
>> </xsd:annotation>
>> <xsd:sequence minOccurs="0" maxOccurs="1">
>> </xsd:element>
>> <xsd:element minOccurs="0" maxOccurs="unbounded"
>> name="userRole" type="secmsec:Role">
>> <xsd:annotation>
>> <xsd:appinfo source="teneo.jpa"> @OneToMany(indexed=false,
>> mappedBy="role") @JoinTable(name="UserRole")</xsd:appinfo>
>> </xsd:annotation>
>> </xsd:element>
>> </xsd:sequence>
>> <xsd:attribute name="id" type="xsd:int">
>> <xsd:annotation>
>> <xsd:appinfo source="teneo.jpa">@Id
>> @GeneratedValue(strategy=IDENTITY)</xsd:appinfo>
>> </xsd:annotation>
>> </xsd:attribute>
>> <xsd:attribute name="firstName" type="xsd:string" />
>> </xsd:complexType>
>> <xsd:element name="user" type="secmsec:User">
>> </xsd:element>
>>
>>
>> <xsd:complexType name="Role">
>> <xsd:sequence minOccurs="0" maxOccurs="1">
>> <xsd:element minOccurs="0" maxOccurs="unbounded"
>> name="permission" type="secmsec:Permission"
>> ecore:opposite="role"/>
>> <xsd:element minOccurs="0" maxOccurs="unbounded"
>> name="roleUser" type="secmsec:User">
>> <xsd:annotation>
>> <xsd:appinfo source="teneo.jpa"> @OneToMany(indexed=false,
>> mappedBy="user") @JoinTable(name="UserRole")</xsd:appinfo>
>> </xsd:annotation>
>> </xsd:element>
>> </xsd:sequence>
>> <xsd:attribute name="id" type="xsd:int">
>> <xsd:annotation>
>> <xsd:appinfo source="teneo.jpa">@Id</xsd:appinfo>
>> </xsd:annotation>
>> </xsd:attribute>
>> <xsd:attribute name="name" type="xsd:string">
>> <xsd:annotation>
>> <xsd:appinfo
>> source="teneo.jpa">@Column(unique=true)</xsd:appinfo>
>> </xsd:annotation>
>> </xsd:attribute>
>> </xsd:complexType>
>>
>> I want to manually control the ids for Role, thus not @GeneratedValue.
>>
>> This generates the required userrole table without problem. However
>> when I try to use Hibernate to populate this table never gets
>> populated. Not sure if it is my use of Hibernate or if I am missing a
>> annotation.
>>
>> I am using this code
>>
>> // We will insert records for each of the cases below
>> Session sess = getCurrentSession();
>> Transaction tx = sess.beginTransaction();
>>
>> try
>> {
>> // add the standard Role objects
>> Role adminRole = secmsecFactory.eINSTANCE.createRole();
>> adminRole.setId(1);
>> adminRole.setName("admin");
>> sess.save(adminRole);
>>
>> // add the standard User for admin
>> User adminUser = secmsecFactory.eINSTANCE.createUser();
>> adminUser.setFirstName("Super");
>> adminUser.setId(1);
>> adminUser.getUserRole().add(adminRole);
>> sess.save(adminUser);
>>
>> tx.commit();
>> ....
>>
>>
>> Thx.
>>
>> David
>
>
Re: [Teneo] Generated Many to Many not populating resolving table [message #424512 is a reply to message #424508] Tue, 28 October 2008 20:01 Go to previous message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi David,
As I have your project/model, can you send me the xsd you currently have then I will check this
issue also.

gr. Martin

David Wynter wrote:
> Hi,
>
> Here is a new problem. I changed that really large xsd to use the
> ManyToMany annotation. The element in question has 17 many to many
> relationships in this large model. The XSD reloads to the genmodel
> without error, then the Model source code generates without error.
>
> But when I come to generate the database schema using the initialise
> method I get this
>
> Exception in thread "main"
> org.eclipse.emf.teneo.annotations.parser.AnnotationParserExc eption: Only
> typenames are allowed at the root of the annotation, see _ for the error
> ManyToMany_(indexed=false) @JoinTable(name="HolderActionSubclassAgent")
>
> here is the call stack
>
> Thread [main] (Suspended (exception AnnotationParserException))
> AnnotationParser.parse(ENamedElement, String) line: 49
> HbEAnnotationParserImporter(EAnnotationParserImporter).proce ss(EAnnotation,
> ENamedElement) line: 159
> HbEAnnotationParserImporter(EAnnotationParserImporter).proce ssAnnotatedModelElement(PAnnotatedEModelElement,
> EPackage) line: 93
> HbEAnnotationParserImporter(EAnnotationParserImporter).proce ss(PAnnotatedEClass)
> line: 83
> HbEAnnotationParserImporter(EAnnotationParserImporter).proce ss(PAnnotatedEPackage)
> line: 72
> HbEAnnotationParserImporter(EAnnotationParserImporter).proce ss(PAnnotatedModel)
> line: 64
> PersistenceMappingBuilder.buildMapping(List<EPackage>,
> PersistenceOptions, ExtensionManager) line: 110
> PersistenceMappingBuilder.buildMapping(EPackage[],
> PersistenceOptions, ExtensionManager) line: 67
> HbSessionDataStore(HbDataStore).mapEPackages() line: 624
> HbSessionDataStore.mapModel() line: 155
> HbSessionDataStore.initialize() line: 73
> HibernateUtil.init(boolean) line: 87
> HibernateUtil.main(String[]) line: 115
>
>
> But this element has identical structure to the others. Here are two of
> them
>
> <xsd:element minOccurs="0" maxOccurs="unbounded"
> ecore:opposite="distributionSubclassAgent"
> name="agentDistributionSubclass" type="xsd:anyURI"
> ecore:reference="gensec:DistributionSubclass">
> <xsd:annotation>
> <xsd:appinfo source="teneo.jpa"> @ManyToMany(indexed=false)
> @JoinTable(name="DistributionSubclassAgent")</xsd:appinfo >
> </xsd:annotation>
> </xsd:element>
> <xsd:element minOccurs="0" maxOccurs="unbounded"
> ecore:opposite="holderActionSubclassAgent"
> name="agentHolderActionSubclass" type="xsd:anyURI"
> ecore:reference="gensec:HolderActionSubclass">
> <xsd:annotation>
> <xsd:appinfo source="teneo.jpa"> @ManyToMany(indexed=false)
> @JoinTable(name="HolderActionSubclassAgent")</xsd:appinfo >
> </xsd:annotation>
> </xsd:element>
>
> Not sure why it has a problem with this particular element.
>
> David
>
> Martin Taal wrote:
>> Hi David,
>> I tested it and the main reason why this does not work is that you
>> have specified mapped-by on both sides. Setting mapped-by on an
>> association means that the association is handled from the other side.
>> Let me explain: say you have an association between types A and B, the
>> efeature on the A side is called toB and on the B side toA. Then if
>> you have a onetomany annotation with mappedby on toB then when
>> persisting A the association to B is not stored because hibernate
>> assumes that B (the other side) manages the relation. This can be
>> solved by setting cascade options on toB also, which will result in B
>> also being persisted (with the relation to A).
>>
>> Using mappedBy makes the most sense if you want to control where the
>> joincolumn is placed in a association using foreign keys, so with a
>> jointable you don't need it. Also the mappedBy you specified had the
>> wrong values afaics it should have been roleUser instead of user and
>> userRole instead of role.
>>
>> Some other things which I would change:
>> - I would not use containment between user and role, afaics these are
>> just standard references between 2 types. In emf
>> references/associations are modeled in xsd using anyuri and
>> ecore:reference
>> - You did not specify ecore:opposite therefore Teneo/EMF can not see
>> that this is a bidirectional relation
>> - You used a OneToMany while you mention that it is a ManyToMany relation
>>
>> I have attached the xsd I have used to get it working correctly, the
>> xsd contains the changes above.
>>
>> Teneo will now throw an exception if you have set mappedby on both
>> sides of the relation as this can lead to difficult to understand issues.
>>
>> gr. Martin
>>
>> David Wynter wrote:
>>> Hi,
>>>
>>> I have these two entities:
>>>
>>> <xsd:complexType name="User">
>>> <xsd:annotation>
>>> <xsd:documentation>
>>> <xsd:p>The basic user entity. Role is a one to many, many to
>>> one association</xsd:p>
>>> </xsd:documentation>
>>> </xsd:annotation>
>>> <xsd:sequence minOccurs="0" maxOccurs="1">
>>> </xsd:element>
>>> <xsd:element minOccurs="0" maxOccurs="unbounded"
>>> name="userRole" type="secmsec:Role">
>>> <xsd:annotation>
>>> <xsd:appinfo source="teneo.jpa"> @OneToMany(indexed=false,
>>> mappedBy="role") @JoinTable(name="UserRole")</xsd:appinfo>
>>> </xsd:annotation>
>>> </xsd:element>
>>> </xsd:sequence>
>>> <xsd:attribute name="id" type="xsd:int">
>>> <xsd:annotation>
>>> <xsd:appinfo source="teneo.jpa">@Id
>>> @GeneratedValue(strategy=IDENTITY)</xsd:appinfo>
>>> </xsd:annotation>
>>> </xsd:attribute>
>>> <xsd:attribute name="firstName" type="xsd:string" />
>>> </xsd:complexType>
>>> <xsd:element name="user" type="secmsec:User">
>>> </xsd:element>
>>>
>>>
>>> <xsd:complexType name="Role">
>>> <xsd:sequence minOccurs="0" maxOccurs="1">
>>> <xsd:element minOccurs="0" maxOccurs="unbounded"
>>> name="permission" type="secmsec:Permission"
>>> ecore:opposite="role"/>
>>> <xsd:element minOccurs="0" maxOccurs="unbounded"
>>> name="roleUser" type="secmsec:User">
>>> <xsd:annotation>
>>> <xsd:appinfo source="teneo.jpa"> @OneToMany(indexed=false,
>>> mappedBy="user") @JoinTable(name="UserRole")</xsd:appinfo>
>>> </xsd:annotation>
>>> </xsd:element>
>>> </xsd:sequence>
>>> <xsd:attribute name="id" type="xsd:int">
>>> <xsd:annotation>
>>> <xsd:appinfo source="teneo.jpa">@Id</xsd:appinfo>
>>> </xsd:annotation>
>>> </xsd:attribute>
>>> <xsd:attribute name="name" type="xsd:string">
>>> <xsd:annotation>
>>> <xsd:appinfo
>>> source="teneo.jpa">@Column(unique=true)</xsd:appinfo>
>>> </xsd:annotation>
>>> </xsd:attribute>
>>> </xsd:complexType>
>>>
>>> I want to manually control the ids for Role, thus not @GeneratedValue.
>>>
>>> This generates the required userrole table without problem. However
>>> when I try to use Hibernate to populate this table never gets
>>> populated. Not sure if it is my use of Hibernate or if I am missing a
>>> annotation.
>>>
>>> I am using this code
>>>
>>> // We will insert records for each of the cases below
>>> Session sess = getCurrentSession();
>>> Transaction tx = sess.beginTransaction();
>>>
>>> try
>>> {
>>> // add the standard Role objects
>>> Role adminRole = secmsecFactory.eINSTANCE.createRole();
>>> adminRole.setId(1);
>>> adminRole.setName("admin");
>>> sess.save(adminRole);
>>>
>>> // add the standard User for admin
>>> User adminUser = secmsecFactory.eINSTANCE.createUser();
>>> adminUser.setFirstName("Super");
>>> adminUser.setId(1);
>>> adminUser.getUserRole().add(adminRole);
>>> sess.save(adminUser);
>>>
>>> tx.commit();
>>> ....
>>>
>>>
>>> Thx.
>>>
>>> David
>>
>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Previous Topic:[CDO] A locked CDOView has objects removed from its model
Next Topic:Memory footprint of common base objects
Goto Forum:
  


Current Time: Wed Apr 24 20:37:39 GMT 2024

Powered by FUDForum. Page generated in 0.02876 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top