-->
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.  [ 9 posts ] 
Author Message
 Post subject: id not auto_incremented
PostPosted: Tue Mar 30, 2004 10:37 am 
Newbie

Joined: Tue Mar 30, 2004 9:32 am
Posts: 6
Location: Bristol, England
I am using Hibernate 2.1.1, mysql 4.0.1 Xdoclet 1.2.

I am using xdoclet and hibernate to create my tabel definitions.. I am having a problem configuring one of my columns to be auto_incrementing..!

My code is as follows..

package com.sovision.cmsCore.persistence;

import org.appfuse.persistence.BaseObject;


/**
* This class represents the website pages.
*
* @hibernate.class table="SubSection_Page"
*
* @author julian
*
*/
public class PageLink extends BaseObject {
///////////////////////////////////////
// attributes
protected Long pageLinkId;
protected Page linkedPage;
protected SubSection subSection;
protected int order;


/**
* Returns the pageLinkId.
*
* @return Long
*
* @hibernate.id generator-class="increment"
*
*
*
*/
public Long getPageLinkId() {
return pageLinkId;
}

/**
* @hibernate.many-to-one column="Page_ID" class="com.sovision.cmsCore.persistence.Page"
*
* @return Page
*
*/
public Page getLinkedPage() {
return linkedPage;
}

/**
* @hibernate.many-to-one column="SubSection_ID" class="com.sovision.cmsCore.persistence.SubSection"
*
* @return SubSection
*
*/
public SubSection getSubSection() {
return subSection;
}

/**
* @hibernate.property column="Ind" not-null="true" insert="false" update="false"
*
* @return int
*/

public int getOrder() {
return order;
}

/**
* @param i
*/
public void setPageLinkId(Long i) {
pageLinkId = i;
}

/**
* @param linkedPage
*/
public void setLinkedPage(Page page) {
this.linkedPage = page;
}

/**
* @param section
*/
public void setSubSection(SubSection section) {
subSection = section;
}

/**
* @param i
*/
public void setOrder(int i) {
order = i;
}

}

What I'm getting is :


<?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="com.sovision.cmsCore.persistence.PageLink"
table="SubSection_Page"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="pageLinkId"
column="pageLinkId"
type="java.lang.Long"
>
<generator class="increment">
</generator>
</id>

<many-to-one
name="linkedPage"
class="com.sovision.cmsCore.persistence.Page"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="Page_ID"
/>

<many-to-one
name="subSection"
class="com.sovision.cmsCore.persistence.SubSection"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="SubSection_ID"
/>

<property
name="order"
type="int"
update="false"
insert="false"
column="Ind"
not-null="true"
/>

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

</class>

</hibernate-mapping>

When I check the table I find that the pageLinkId column is not configured to auto_increment and there is a composite primary key which consists of SubSection_ID and Ind.

What am I doing wrong ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 9:50 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use identity, increment is a JVM increment. Refer to the reference guide for more info on generators

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 10:05 am 
Newbie

Joined: Tue Mar 30, 2004 9:32 am
Posts: 6
Location: Bristol, England
Tried it with the identity generator class and i got the subsection_id to be auto_incrementing.. when all I want to do is for the pageLinkId to be auto_incrementing..!

package com.sovision.cmsCore.persistence;

import org.appfuse.persistence.BaseObject;


/**
* This class represents the website pages.
*
* @hibernate.class table="SubSection_Page"
*
* @author julian
*
*/
public class PageLink extends BaseObject {
///////////////////////////////////////
// attributes
protected Long pageLinkId;
protected Page linkedPage;
protected SubSection subSection;
protected int order;



/**
* Returns the pageLinkId.
*
* @return Long
*
* @hibernate.id generator-class="identity"
*
*
*/
public Long getPageLinkId() {
return pageLinkId;
}

/**
* @hibernate.many-to-one column="Page_ID" class="com.sovision.cmsCore.persistence.Page"
*
* @return Page
*
*/
public Page getLinkedPage() {
return linkedPage;
}

/**
* @hibernate.many-to-one column="SubSection_ID" class="com.sovision.cmsCore.persistence.SubSection"
*
* @return SubSection
*
*/
public SubSection getSubSection() {
return subSection;
}

/**
* @hibernate.property column="Ind" not-null="true" insert="false" update="false"
*
* @return int
*/

public int getOrder() {
return order;
}

/**
* @param i
*/
public void setPageLinkId(Long i) {
pageLinkId = i;
}

/**
* @param linkedPage
*/
public void setLinkedPage(Page page) {
this.linkedPage = page;
}

/**
* @param section
*/
public void setSubSection(SubSection section) {
subSection = section;
}

/**
* @param i
*/
public void setOrder(int i) {
order = i;
}

}

Schema Tables:

[schemaexport] create table SubSection (
[schemaexport] subSectionId BIGINT NOT NULL AUTO_INCREMENT,
[schemaexport] live BIT not null,
[schemaexport] section BIGINT not null,
[schemaexport] title VARCHAR(255) not null,
[schemaexport] url VARCHAR(255),
[schemaexport] ord INTEGER not null,
[schemaexport] list BIT not null,
[schemaexport] restricted BIT not null,
[schemaexport] primary key (subSectionId)
[schemaexport] )
[schemaexport] create table Section (
[schemaexport] sectionId BIGINT NOT NULL AUTO_INCREMENT,
[schemaexport] title VARCHAR(60) not null,
[schemaexport] ord INTEGER,
[schemaexport] live BIT not null,
[schemaexport] primary key (sectionId)
[schemaexport] )
[schemaexport] create table user_role (
[schemaexport] id BIGINT NOT NULL AUTO_INCREMENT,
[schemaexport] user_id BIGINT not null,
[schemaexport] username VARCHAR(255) not null,
[schemaexport] role_name VARCHAR(255) not null,
[schemaexport] primary key (id)
[schemaexport] )
[schemaexport] create table app_user (
[schemaexport] id BIGINT NOT NULL AUTO_INCREMENT,
[schemaexport] username VARCHAR(255) not null unique,
[schemaexport] password VARCHAR(255) not null,
[schemaexport] firstName VARCHAR(255) not null,
[schemaexport] lastName VARCHAR(255) not null,
[schemaexport] address VARCHAR(255),
[schemaexport] city VARCHAR(255) not null,
[schemaexport] country VARCHAR(100),
[schemaexport] email VARCHAR(255) unique,
[schemaexport] phoneNumber VARCHAR(255),
[schemaexport] postalCode VARCHAR(255) not null,
[schemaexport] province VARCHAR(100),
[schemaexport] website VARCHAR(255),
[schemaexport] passwordHint VARCHAR(255),
[schemaexport] primary key (id)
[schemaexport] )
[schemaexport] create table role (
[schemaexport] name VARCHAR(255) not null,
[schemaexport] description VARCHAR(255),
[schemaexport] primary key (name)
[schemaexport] )
[schemaexport] create table SubSection_Page (
[schemaexport] pageLinkId BIGINT not null,
[schemaexport] Page_ID BIGINT,
[schemaexport] SubSection_ID BIGINT NOT NULL AUTO_INCREMENT,
[schemaexport] Ind INTEGER not null,
[schemaexport] primary key (SubSection_ID, Ind)
[schemaexport] )
[schemaexport] create table Page (
[schemaexport] pageId BIGINT NOT NULL AUTO_INCREMENT,
[schemaexport] modified DATETIME not null,
[schemaexport] User_ID BIGINT,
[schemaexport] content TEXT,
[schemaexport] live BIT not null,
[schemaexport] title VARCHAR(255) not null,
[schemaexport] primary key (pageId)
[schemaexport] )
[schemaexport] alter table SubSection add index (section), add constraint FK93F1ACA5756F7EE5 foreign key (section) references Section (sectionId)
[schemaexport] alter table SubSection_Page add index (SubSection_ID), add constraint FKDB2A020968EB42F5 foreign key (SubSection_ID) references SubSection (subSectionId)
[schemaexport] alter table SubSection_Page add index (Page_ID), add constraint FKDB2A0209335364AB foreign key (Page_ID) references Page (pageId)
[schemaexport] alter table Page add index (User_ID), add constraint FK25D6AF5A7381EF foreign key (User_ID) references app_user (id)
[schemaexport] (hbm2ddl.SchemaExport 160 ) schema export complete
[schemaexport] (connection.DriverManagerConnectionProvider 137 ) cleaning up connection pool: jdbc:mysql://localhost:3306/socms?autoReconnect=true
BUILD SUCCESSFUL
Total time: 31 seconds

Julian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 10:21 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You don't show the subsection mapping so I can't tell.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 10:27 am 
Newbie

Joined: Tue Mar 30, 2004 9:32 am
Posts: 6
Location: Bristol, England
Here's the subsection java and mapping file..

package com.sovision.cmsCore.persistence;

import java.util.List;

import org.appfuse.persistence.BaseObject;

/**
* First node after Section in the CMS. It's parent is a Section. Child
* nodes are PageLinks which
*
* @author steve
*
* @hibernate.class table="SubSection"
*
* @struts.form include-all="true" extends="org.appfuse.webapp.form.BaseForm"
*
*/

public class SubSection extends BaseObject {
///////////////////////////////////////
// attributes
protected Long subSectionId;
protected Section section;
protected String title;
protected boolean live;
protected String url;
protected List pages;
protected List pageLinks;
protected int order;
protected Long SectionId;
protected boolean list;
protected boolean restricted;

/**
* Returns the id.
* @return String
*
* @hibernate.id generator-class="native" unsaved-value="null"
*
*
*/
public Long getSubSectionId() {
return subSectionId;
}

/**
* @hibernate.property not-null="true"
*
* @return boolean
*
*/
public boolean isLive() {
return live;
}

/**
* @hibernate.many-to-one class="com.sovision.cmsCore.persistence.Section" not-null="true"
*
* @return Section
*
*/
public Section getSection() {
return section;
}

/**
* @hibernate.property not-null="true"
*
* @struts.validator type="required" msgkey="errors.required"
*
* @return String
*/
public String getTitle() {
return title;
}

/**
* @hibernate.property
*
* @return String
*/
public String getUrl() {
return url;
}

/**
* @hibernate.list name="getPages" table="SubSection_Page" lazy="true"
* @hibernate.collection-key column="SubSection_ID"
* @hibernate.collection-index column="Ind"
* @hibernate.collection-many-to-many class="com.sovision.cmsCore.persistence.Page" column="Page_ID"
*
* @return List
*/
public List getPages() {
return pages;
}

/**
* @hibernate.list name="getPageLinks" lazy="true" inverse="true"
* @hibernate.collection-key column="SubSection_ID"
* @hibernate.collection-index column="Ind"
* @hibernate.collection-one-to-many class="com.sovision.cmsCore.persistence.PageLink"
*
* @return List
*/
public List getPageLinks() {
return pageLinks;
}


/**
* @hibernate.property column="ord" insert="false" update="false" not-null="true"
*
* @return int
*/
public int getOrder() {
return order;
}


/**
* @hibernate.property column="section" insert="false" update="false"
*
* @return Long
*/
public Long getSectionId() {
return SectionId;
}


/**
* @hibernate.property not-null="true"
*
* @return boolean
*/
public boolean isList() {
return this.list;
}

/**
* @hibernate.property not-null="true"
*
* @return boolean
*/
public boolean isRestricted() {
return restricted;
}



/**
* Returns the number of pages in this subSection
*
* @return int
*/
public int getPageCount() {
return this.pages.size();
}


/**
* @param i
*/
public void setSubSectionId(Long i) {
subSectionId = i;
}

/**
* @param b
*/
public void setLive(boolean b) {
this.live = b;
}

/**
* @param section
*/
public void setSection(Section section) {
this.section = section;
}

/**
* @param string
*/
public void setTitle(String string) {
title = string;
}

/**
* @param string
*/
public void setUrl(String string) {
url = string;
}

/**
* @param list
*/
public void setPages(List list) {
pages = list;
}

/**
* @param page
*/
public void addPage(Page page) {
pages.add(page);
}

/**
* @param i
*/
public void setOrder(int i) {
order = i;
}

/**
* @param list
*/
public void setPageLinks(List list) {
pageLinks = list;
}

/**
* @param long1
*/
public void setSectionId(Long long1) {
SectionId = long1;
}

/**
* @param list The list to set.
*/
public void setList(boolean list) {
this.list = list;
}

/**
* @param restricted The restricted to set.
*/
public void setRestricted(boolean restricted) {
this.restricted = restricted;
}

}

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="com.sovision.cmsCore.persistence.SubSection"
table="SubSection"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="subSectionId"
column="subSectionId"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="native">
</generator>
</id>

<property
name="live"
type="boolean"
update="true"
insert="true"
column="live"
not-null="true"
/>

<many-to-one
name="section"
class="com.sovision.cmsCore.persistence.Section"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="section"
not-null="true"
/>

<property
name="title"
type="java.lang.String"
update="true"
insert="true"
column="title"
not-null="true"
/>

<property
name="url"
type="java.lang.String"
update="true"
insert="true"
column="url"
/>

<list
name="pages"
table="SubSection_Page"
lazy="true"
inverse="false"
cascade="none"
>

<key
column="SubSection_ID"
/>

<index
column="Ind"
/>

<many-to-many
class="com.sovision.cmsCore.persistence.Page"
column="Page_ID"
outer-join="auto"
/>

</list>

<list
name="pageLinks"
lazy="true"
inverse="true"
cascade="none"
>

<key
column="SubSection_ID"
/>

<index
column="Ind"
/>

<one-to-many
class="com.sovision.cmsCore.persistence.PageLink"
/>
</list>

<property
name="order"
type="int"
update="false"
insert="false"
column="ord"
not-null="true"
/>

<property
name="sectionId"
type="java.lang.Long"
update="false"
insert="false"
column="section"
/>

<property
name="list"
type="boolean"
update="true"
insert="true"
column="list"
not-null="true"
/>

<property
name="restricted"
type="boolean"
update="true"
insert="true"
column="restricted"
not-null="true"
/>

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

</class>

</hibernate-mapping>


Julian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 3:34 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
/**
* Returns the id.
* @return String
*
* @hibernate.id generator-class="native" unsaved-value="null"
*
*
*/
public Long getSubSectionId() {
return subSectionId;
}


You"re using native, si identity is used in your DB, this is expected to have a SubSectionId auto-incremented

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 3:46 am 
Newbie

Joined: Tue Mar 30, 2004 9:32 am
Posts: 6
Location: Bristol, England
What I want is the subsection_id to be auto_incremented in the subsection table but not in the subsection_page table as I want the pageLinkId to be auto_incrementing in the subsection_page table and add it to the primary key..

Regards

Julian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 4:21 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You're using the subsection_page table for entity persistence and relation persistenbce (many-to-many) you're doing something forbidden.
Use a separate assocuation table

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 5:32 am 
Newbie

Joined: Tue Mar 30, 2004 9:32 am
Posts: 6
Location: Bristol, England
Thanks for pointing me in the right direction..!!

Julian


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

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.