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
|