-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can I map One class to different tables in hibernate
PostPosted: Sun Oct 18, 2009 1:48 am 
Newbie

Joined: Sun Oct 18, 2009 1:44 am
Posts: 8
Hello All,

I have a scenario where to one class there are many tables mapped. Can I map one class name to different tables in the (xyz.hbm.xml) config file?


THanks


Top
 Profile  
 
 Post subject: Re: Can I map One class to different tables in hibernate
PostPosted: Mon Oct 19, 2009 6:03 am 
Beginner
Beginner

Joined: Wed Jun 17, 2009 9:03 pm
Posts: 31
Location: mumbai
Hi

Check if you are looking for something like this

Code:
<hibernate-mapping>
<class name="com.Item" entity-name="ItemAuction" table="ITEM_AUCTION">
   <id name="id" column="ITEM_AUCTION_ID">...</id>
   <property name="xyz" column="xyz"/>
</class>
<class name="com.Item" entity-name="ItemSale" table="ITEM_SALE">
   <id name="id" column="ITEM_SALE_ID">...</id>
   <property name="xyz" column="xyz"/>
</class>
</hibernate-mapping>


and then save it like

Code:
Item itemForAuction = new Item();
itemForAuction.setDescription("desc");
itemForAuction.setInitialPrice( new BigDecimal(11) );
session.save("ItemAuction", itemForAuction);


Top
 Profile  
 
 Post subject: Re: Can I map One class to different tables in hibernate
PostPosted: Mon Oct 19, 2009 3:41 pm 
Newbie

Joined: Sun Oct 18, 2009 1:44 am
Posts: 8
prateek.singh wrote:
Hi

Check if you are looking for something like this

Code:
<hibernate-mapping>
<class name="com.Item" entity-name="ItemAuction" table="ITEM_AUCTION">
   <id name="id" column="ITEM_AUCTION_ID">...</id>
   <property name="xyz" column="xyz"/>
</class>
Thanks Prateek!

What is this entity name? COuld you please expand on that ?





<class name="com.Item" entity-name="ItemSale" table="ITEM_SALE">
   <id name="id" column="ITEM_SALE_ID">...</id>
   <property name="xyz" column="xyz"/>
</class>
</hibernate-mapping>


and then save it like

Code:
Item itemForAuction = new Item();
itemForAuction.setDescription("desc");
itemForAuction.setInitialPrice( new BigDecimal(11) );
session.save("ItemAuction", itemForAuction);


Top
 Profile  
 
 Post subject: Re: Can I map One class to different tables in hibernate
PostPosted: Mon Oct 19, 2009 3:57 pm 
Newbie

Joined: Sun Oct 18, 2009 1:44 am
Posts: 8
My java class is as follows:
package org.example;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
* <p>Java class for companyType complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType name="companyType">
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="address" type="{http://www.w3.org/2001/XMLSchema}anyType"/>
* &lt;element ref="{}contact"/>
* &lt;/sequence>
* &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "companyType", propOrder = {
"address",
"contact"
})
public class CompanyType {

@XmlElement(required = true)
protected Object address;
@XmlElement(required = true)
protected ContactType contact;
@XmlAttribute
protected String name;

/**
* Gets the value of the address property.
*
* @return
* possible object is
* {@link Object }
*
*/
public Object getAddress() {
return address;
}

/**
* Sets the value of the address property.
*
* @param value
* allowed object is
* {@link Object }
*
*/
public void setAddress(Object value) {
this.address = value;
}

/**
* Gets the value of the contact property.
*
* @return
* possible object is
* {@link ContactType }
*
*/
public ContactType getContact() {
return contact;
}

/**
* Sets the value of the contact property.
*
* @param value
* allowed object is
* {@link ContactType }
*
*/
public void setContact(ContactType value) {
this.contact = value;
}

/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}

/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}

}



How should I create a table in the database and how do I populate values? Please help!!!


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