-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping file for existing table - two fields as primary key
PostPosted: Tue Sep 05, 2006 8:59 am 
Beginner
Beginner

Joined: Thu Aug 04, 2005 5:06 am
Posts: 31
Location: Bedford, UK
Hibernate version: 3.1.3

Database: MySQL 5.0

So far I have only created simple tables with an Id autoincrementing primary key.

I have an existing table for which I urgently need to generate a mapping with the last two fields being the primary key as follows:

-- Table structure for advertiser
-- ----------------------------
CREATE TABLE `advertiser` (
`advertiserMasterId` int(11) default NULL,
`advertiserId` int(11) NOT NULL,
`networkId` int(11) NOT NULL,
PRIMARY KEY (`advertiserId`,`networkId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

How should the hibernate mapping file be defined to match this structure, please?

Regards.

Mark


Top
 Profile  
 
 Post subject: See composite-id
PostPosted: Tue Sep 05, 2006 9:20 am 
Regular
Regular

Joined: Wed Aug 24, 2005 11:49 am
Posts: 63
http://www.hibernate.org/hib_docs/v3/re ... ompositeid


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 7:42 am 
Beginner
Beginner

Joined: Thu Aug 04, 2005 5:06 am
Posts: 31
Location: Bedford, UK
Thanks for the pointer ... I usually work best with good examples which I did not find at that location hence the question.

With a bit more reading and trials I found a solution as follows.

Advertiser.hbm.xml
***************************************
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="com.xxx.models.Advertiser" table="advertiser">
<meta attribute="class-description">
Represents a single Advertiser.
</meta>

<composite-id
name="id"
class="com.dgmpro.models.AdvertiserPK" >

<key-property name="advertiserId" />
<key-property name="networkId" />

</composite-id>

<property name="advertiserMasterId" type="integer" not-null="true">
<meta attribute="field-description">Advertiser Master Id</meta>
</property>

<property name="advertiserId" type="integer" not-null="true"
insert="false" update="false">
<meta attribute="field-description">Network Advertiser Id</meta>
</property>

<property name="networkId" type="integer" not-null="true"
insert="false" update="false">
<meta attribute="field-description">Network Id</meta>
</property>

</class>
</hibernate-mapping>
***************************************

AdvertiserPK.java
***************************************
package com.xxx.models;

/**
* AdvertiserPK - primary key.
*/
public class AdvertiserPK implements java.io.Serializable {

// Fields

private int advertiserId;
private int networkId;

// Constructors

/** default constructor */
public AdvertiserPK() {
}

/** full constructor */
public AdvertiserPK(int advertiserId, int networkId) {
this.advertiserId = advertiserId;
this.networkId = networkId;
}


// Property accessors
public int getAdvertiserId() {
return this.advertiserId;
}

public void setAdvertiserId(int advertiserId) {
this.advertiserId = advertiserId;
}
public int getNetworkId() {
return this.networkId;
}

public void setNetworkId(int networkId) {
this.networkId = networkId;
}

}
***************************************


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.