-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Help: xdoclet+schema generator for many-to-many association
PostPosted: Sat Jun 26, 2004 6:36 am 
Newbie

Joined: Sat Jun 26, 2004 6:12 am
Posts: 1
Hi,

I'm using hibernate 2.1.4, xdoclet 1.2.1 and i'm trying to map a unidirectional many-to-many association. Xdoclet runs without any problems. The mapping files seems to be ok. Next I run the schema export tool, and when I check de mapping files, the many-to-many table, does not have a primary key.

Here's my domain object:

package domain.admin;

import java.util.List;

import domain.DomainObject;

/**
*
* @hibernate.class table = "USER"
*
* @hibernate.cache usage="read-write"
*
*
*/
public class User extends DomainObject
{

private String username;

private String password;

private List credentials;

private List accounts;

public User()
{
}

/**
* @hibernate.property type = "string"
*
* @hibernate.column name = "PASSWORD"
* length = "50"
* not-null = "true"
* @return Returns the password.
*/
public String getPassword()
{
return password;
}

/**
* @param password
* The password to set.
*/
public void setPassword(String password)
{
this.password = password;
}

/**
* @hibernate.property type = "string"
*
* @hibernate.column name = "USERNAME"
* length = "50"
* not-null = "true"
* unique-key = "u1"
*/
public String getUsername()
{
return username;
}

/**
* @param username
* The username to set.
*/
public void setUsername(String username)
{
this.username = username;
}

/**
* @hibernate.bag table = "USER_CREDENTIAL"
* cascade = "none"
* lazy = "true"
* order-by = "KEY_CREDENTIAL asc"
*
* @hibernate.collection-key column = "KEY_USER"
*
* @hibernate.collection-many-to-many
* class= "domain.admin.Credential"
* column = "KEY_CREDENTIAL"
*
*
* @return Returns the credentials.
*/
public List getCredentials()
{
return credentials;
}

/**
* @param credentials The credentials to set.
*/
public void setCredentials(List credentials)
{
this.credentials = credentials;
}

/**
* @hibernate.bag table = "ACCOUNTS"
* cascade = "delete"
* lazy = "true"
* order-by="ID_INTERNAL asc"
*
* @hibernate.collection-key column = "KEY_USER"
*
* @hibernate.collection-one-to-many class = "domain.Account"
*
* @return Returns the accounts.
*/
public List getAccounts()
{
return accounts;
}

/**
* @param accounts The accounts to set.
*/
public void setAccounts(List accounts)
{
this.accounts = accounts;
}

public boolean equals(Object obj)
{
boolean result = false;
if (obj instanceof User)
{
User user = (User) obj;
result = this.username.equals(user.getUsername())
&& this.password.equals(user.getPassword());
}

return result;
}

}




Here's the generated mapping file:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="domain.admin.User"
table="USER"
dynamic-update="false"
dynamic-insert="false"
>
<cache usage="read-write" />

<id
name="idInternal"
column="ID_INTERNAL"
type="long"
>
<generator class="identity">
</generator>
</id>

<version
name="ackOptLock"
type="long"
column="ACK_OPT_LOCK"
access="property"
unsaved-value="null"
/>

<property
name="password"
type="string"
update="true"
insert="true"
access="property"
>
<column
name="PASSWORD"
length="50"
not-null="true"
/>
</property>

<property
name="username"
type="string"
update="true"
insert="true"
access="property"
>
<column
name="USERNAME"
length="50"
unique-key="u1"
not-null="true"
/>
</property>

<bag
name="credentials"
table="USER_CREDENTIAL"
lazy="true"
inverse="false"
cascade="none"
order-by="KEY_CREDENTIAL asc"
>

<key
column="KEY_USER"
>
</key>

<many-to-many
class="domain.admin.Credential"
column="KEY_CREDENTIAL"
outer-join="auto"
/>

</bag>

<bag
name="accounts"
table="ACCOUNTS"
lazy="true"
inverse="false"
cascade="delete"
order-by="ID_INTERNAL asc"
>

<key
column="KEY_USER"
>
</key>

<one-to-many
class="domain.Account"
/>
</bag>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-User.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>



Here's the schema generator ant task:

<target name="hibernate-schema-export">
<taskdef name="schemaexport"
classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="project.libs"
classpath="${classes}" />
<schemaexport
properties="${config}/hibernate.properties"
quiet="no"
text="yes"
drop="no"
delimiter=";"
output="scripts/${generated.sql.file}" >
<fileset dir="config">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaexport>
</target>


Finally heres the generated DDL:

create table USER (
ID_INTERNAL BIGINT NOT NULL AUTO_INCREMENT,
ACK_OPT_LOCK BIGINT not null,
PASSWORD VARCHAR(50) not null,
USERNAME VARCHAR(50) not null,
primary key (ID_INTERNAL),
unique (USERNAME)
);

create table USER_CREDENTIAL (
KEY_USER BIGINT not null,
KEY_CREDENTIAL BIGINT not null
); //////MISSING PRIMARY KEY (KEY_USER,KEY_CREDENTIAL)

alter table USER_CREDENTIAL add index FK606894B4E4809CB (KEY_USER), add constraint FK606894B4E4809CB foreign key (KEY_USER) references USER (ID_INTERNAL);
alter table USER_CREDENTIAL add index FK606894B57197A57 (KEY_CREDENTIAL), add constraint FK606894B57197A57 foreign key (KEY_CREDENTIAL) references CREDENTIAL (ID_INTERNAL);


P.S.: I'm using mysql 4.01


Thanks,
Nuno


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.