aspose file tools*
The moose likes Object Relational Mapping and the fly likes need help in JPA ANNOTATION regarding Inheritance. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "need help in JPA ANNOTATION regarding Inheritance." Watch "need help in JPA ANNOTATION regarding Inheritance." New topic
Author

need help in JPA ANNOTATION regarding Inheritance.

Philip Zac
Ranch Hand

Joined: Apr 27, 2008
Posts: 66
I am a newbie here.
I have trying to convert old hibernate to JPA Annotation.
the issue is When i tried to do DiscriminatorValue for a model, it wont show up in the database table.

Here is the model
@Entity
@DiscriminatorValue("CurrencyType")
public class CurrencyTypeComposite extends ContractComponent {

/**
* serialVersionUID.
*/
private static final long serialVersionUID = 1L;

/**
* currencyType.
*/
private String currencyType;

/**
* Full Constructor.
*/
public CurrencyTypeComposite(String currencyType, List<ContractComponent> list) {
this.currencyType = currencyType;
this.setChildList(list);
}

/**
* Default constructor .
*/
public CurrencyTypeComposite() {

}


/**
* The currency type linked to the contract composite.
*/
@Column(name = "currency_type", length = 250, nullable = true, columnDefinition = "VARCHAR(250)")
public String getCurrencyType() {
return currencyType;
}

/**
* Sets the currencyType as String.
*/
public void setCurrencyType(String currencyType) {
this.currencyType = currencyType;
}

/**
* Method to add a new contract composite.
*/
@Transient
public void add(ContractComponent child) {
this.getChildList().add(child);
}

/**
* Method to add a collection of children.
*/
@Transient
public void addAll(ArrayList<ContractComponent> children) {
this.getChildList().addAll(children);
}

/**
* Gets the id of the Composite object id.
*/
@Transient
public Long getObjectId() {
return new Long(0);
}

/**
* Gets the name of the Composite object.
*/
@Transient
public String getObjectName() {
return this.currencyType;
}

public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CurrencyTypeComposite)) return false;

CurrencyTypeComposite that = (CurrencyTypeComposite) o;

if (!currencyType.equals(that.currencyType)) return false;

return true;
}

public int hashCode() {
return currencyType.hashCode();
}


public String toString() {
return "CurrencyTypeComposite{" +
"currencyType='" + currencyType + '\'' +
'}';
}
}


and another model

@Entity
@Table(name="contract_composite")
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator", discriminatorType = DiscriminatorType.STRING,length = 20)
public abstract class ContractComponent {

............
.
................
.
.
.........
}

The database shown is

mysql> desc contract_composite;
+--------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+-------------+------+-----+---------+----------------+
| discriminator | varchar(20) | NO | | | |
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| is_leaf | tinyint(1) | NO | | | |
| week_definition_id | bigint(20) | YES | MUL | NULL | |
| parent_id | bigint(20) | YES | MUL | NULL | |
| room_type_id | bigint(20) | NO | MUL | | |
| season_id | bigint(20) | NO | MUL | | |
| plan_id | bigint(20) | NO | MUL | | |
| hotel_id | bigint(20) | NO | MUL | | |
| rate_id | bigint(20) | NO | MUL | | |
+--------------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

Which isnt correct....

The correct database should be given below......
mysql> desc contract_composite;
+--------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| discriminator | varchar(255) | NO | | | |
| is_leaf | char(1) | YES | | NULL | |
| parent_id | bigint(20) | YES | MUL | NULL | |
| plan_id | bigint(20) | YES | MUL | NULL | |
| room_type_id | bigint(20) | YES | MUL | NULL | |
| currency_type | varchar(250) | YES | | NULL | (* ** This is missing ) |
| hotel_id | bigint(20) | YES | MUL | NULL | |
| rate_id | bigint(20) | YES | MUL | NULL | |
| season_id | bigint(20) | YES | MUL | NULL | |
| week_definition_id | bigint(20) | YES | MUL | NULL | |
+--------------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)


Hope you understood what i mean..... Thanks alot in advance....
Pedro Erencia
Ranch Hand

Joined: Apr 03, 2008
Posts: 70
I've c&p your code.
Added the id in the ContractComponent and getChild, setChild on CurrencyTypeComposite.

I get the table correctly:



I've used JBoss and
[CODE]
<property name="hibernate.hbm2ddl.auto" value="create"/>
[CODE]
on persistence.xml

Are all the imports of the annotations from javax.persistence package ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

"Philip Philip",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Philip Zac
Ranch Hand

Joined: Apr 27, 2008
Posts: 66
Thanks I found the solution. it was due to wrong mapping class.

Anyway. Thank you.

Philip
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help in JPA ANNOTATION regarding Inheritance.