• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help in JPA ANNOTATION regarding Inheritance.

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Philip Zac
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I found the solution. it was due to wrong mapping class.

Anyway. Thank you.

Philip
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic