-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with component mapping
PostPosted: Tue Nov 02, 2004 11:14 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Hibernate version: 2.1.2

I have a class named "Currency", which has a property named "bigDecimal" which is of type BigDecimal.

I want to map a property ("amount" of a class "LandValue" that is of type "Currency") as a Hibernate component.

I figured this HB mapping would do the trick:
Code:
<class name="LandValue">
    ...
    <component name="amount" class="Currency">
        <property name="bigDecimal" column="m_amount"/>
    </component>
</class>


(Can anyone confirm for me that if I can generate that mapping in the XML that it will work?)

The current problem is, I can't for the life of me figure out how I would write the XDoclet attributes to generate the mapping.
I tried:
Code:
* @hibernate.component class="Currency"
* @hibernate.property name="bigDecimal" column="m_amount"


But that didn't generate anything at all in the LandValue mapping file. Can anyone tell me how I might wirte the appropriate XDoclet mapping?

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 5:32 am 
Newbie

Joined: Thu May 13, 2004 8:56 am
Posts: 9
Location: Cape Town, South Africa
Hi,

public class Currency {

private BigDecimal amount;

/**
* @hibernate.property
* @hibernate.column name = "m_amount" sql-type = "numeric(19,4)"
*/
public BigDecimal getAmount() {
return this.amount;
}

public void setAmount(BigDecimal amount) {
this.amount = amount;
}

}

/**
* @hibernate.class
*/
public class LandValue {

private Long id;
private Currency value;

/**
* @hibernate.component
*/
public Currency getValue() {
return this.value;
}

public void setValue(Currency value) {
this.value = value;
}

/**
* @hibernate.id generator-class = "native"
*/
public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

}

the above generates schema:
create table LandValue (
id numeric(19,0) identity not null,
m_amount numeric(19,4) null,
primary key (id)
);[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 6:53 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Yeah, sorry about that, I should have explained better. Problem is, I actually have 15 or so different Currency components of the LandValue class, so the column name can't be embedded in the component mapping itself (since I want to reuse the same component for different columns).

There does appear to be some kind of support for this in XDoclet, but it involves column prefixes, which just won't fit with the (legacy) schema I have to work with.

The current plan is to do this with a CompositeUserType, I'm pretty confident it will work, just yet to get around to it.

Thanks anyway.

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 3:13 am 
Newbie

Joined: Thu May 13, 2004 8:56 am
Posts: 9
Location: Cape Town, South Africa
Hi,

Not a prob, you do it like this:

public class Currency {

private BigDecimal amount;

/**
* @hibernate.property
* @hibernate.column name = "m_amount" sql-type = "numeric(19,4)"
*/
public BigDecimal getAmount() {
return this.amount;
}

public void setAmount(BigDecimal amount) {
this.amount = amount;
}

}

/**
* @hibernate.class
*/
public class LandValue {

private Long id;
private Currency someValue;
private Currency anotherValue;

/**
* @hibernate.component prefix = "somePrefix"
*/
public Currency getSomeValue() {
return this.SomeValue;
}

public void setSomeValue(Currency someValue) {
this.someValue = someValue;
}

/**
* @hibernate.component prefix = "anotherPrefix"
*/
public Currency getAnotherValue() {
return this.anotherValue;
}

public void setAnotherValue(Currency anotherValue) {
this.anotherValue = anotherValue;
}

/**
* @hibernate.id generator-class = "native"
*/
public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

}

the above generates schema:
create table LandValue (
id numeric(19,0) identity not null,
somePrefixm_amount numeric(19,4) null,
anotherPrefixm_amount numeric(19,4) null,
primary key (id)
);

;-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 3:28 am 
Newbie

Joined: Thu May 13, 2004 8:56 am
Posts: 9
Location: Cape Town, South Africa
I'm working on/experimenting at the moment, so here is one of the ways I've used this myself. Three classes: User, UserDate (to be used as a component), BatchStatus (which has several UserDate components in it), and Batch (which has a BatchStatus component).

//USER
Code:
/**
* @author BruceS
* @hibernate.class
*/
public class User implements Serializable {

    private Long id;
    private String name;
    /**
     *
     */
    public User() {
   
    }
   
    /**
     * @hibernate.id generator-class = "native"
     * @return Returns the id.
     */
    public Long getId() {
        return id;
    }
   
    /**
     * @hibernate.property
     * @return Returns the name.
     */
    public String getName() {
        return name;
    }
    /**
     * @param id The id to set.
     */
    public void setId(Long id) {
        this.id = id;
    }
    /**
     * @param name The name to set.
     */
    public void setName(String name) {
        this.name = name;
    }
}


//USERDATE
Code:
/**
* @author BruceS
* NOTE columns names are in proper case. 
* They will be prefixed with lowercase letters where this
* class is used as a component.
*/
public class UserDate {

    private User user;
    private Date date;
   
    /**
     * @hibernate.property
     * @hibernate.column name = "Date" sql-type = "datetime" not-null = "false"
     * @return Returns the date.
     */
    public Date getDate() {
        return date;
    }
    /**
     * @hibernate.many-to-one   
     * @hibernate.column name = "User" not-null = "false"
     * @return Returns the user.
     */
    public User getUser() {
        return user;
    }
    /**
     * @param date The date to set.
     */
    public void setDate(Date date) {
        this.date = date;
    }
    /**
     * @param user The user to set.
     */
    public void setUser(User user) {
        this.user = user;
    }
}


//BATCHSTATUS
Code:
/**
* @author BruceS
*/
public abstract class BatchStatus {

    private UserDate accepted;
    private UserDate checked;
    private UserDate created;
    private UserDate hold;
    private UserDate modified;
    private UserDate opened;
    private UserDate posted;
    private UserDate received;
   
    /**
     * @hibernate.component prefix = "accepted"
     * @return Returns the accepted.
     */
    public UserDate getAccepted() {
        return accepted;
    }
    /**
     * @hibernate.component prefix = "checked"
     * @return Returns the checked.
     */
    public UserDate getChecked() {
        return checked;
    }
    /**
     * @hibernate.component prefix = "created"
     * @return Returns the created.
     */
    public UserDate getCreated() {
        return created;
    }
    /**
     * @hibernate.component prefix = "hold"
     * @return Returns the hold.
     */
    public UserDate getHold() {
        return hold;
    }
    /**
     * @hibernate.component prefix = "modified"
     * @return Returns the modified.
     */
    public UserDate getModified() {
        return modified;
    }
    /**
     * @hibernate.component prefix = "opened"
     * @return Returns the opened.
     */
    public UserDate getOpened() {
        return opened;
    }
    /**
     * @hibernate.component prefix = "posted"
     * @return Returns the posted.
     */
    public UserDate getPosted() {
        return posted;
    }
    /**
     * @hibernate.component prefix = "received"
     * @return Returns the received.
     */
    public UserDate getReceived() {
        return received;
    }

   
    /**
     * @param accepted The accepted to set.
     */
    public void setAccepted(UserDate accepted) {
        this.accepted = accepted;
    }
    /**
     * @param checked The checked to set.
     */
    public void setChecked(UserDate checked) {
        this.checked = checked;
    }
    /**
     * @param created The created to set.
     */
    public void setCreated(UserDate created) {
        this.created = created;
    }
    /**
     * @param hold The hold to set.
     */
    public void setHold(UserDate hold) {
        this.hold = hold;
    }
    /**
     * @param modified The modified to set.
     */
    public void setModified(UserDate modified) {
        this.modified = modified;
    }
    /**
     * @param opened The opened to set.
     */
    public void setOpened(UserDate opened) {
        this.opened = opened;
    }
    /**
     * @param posted The posted to set.
     */
    public void setPosted(UserDate posted) {
        this.posted = posted;
    }
    /**
     * @param received The received to set.
     */
    public void setReceived(UserDate received) {
        this.received = received;
    }

}


//BATCH
Code:
/**
* @author BruceS
* @hibernate.class
*/
public abstract class Batch implements Serializable {
   
    private BatchStatus batchStatus;
    private Long id;
   
    /**
     * @return Returns the theStatus.
     * @hibernate.component
     */
    public BatchStatus getBatchStatus() {
        return batchStatus;
    }
    /**
     * @hibernate.id generator-class = "native"
     * @return Returns the id.
     */
    public Long getId() {
        return id;
    }
    /**
     * @param theStatus The theStatus to set.
     */
    public void setBatchStatus(BatchStatus batchStatus) {
        this.batchStatus = batchStatus;
    }

    /**
     * @param id The id to set.
     */
    public void setId(Long id) {
        this.id = id;
    }

}


All the above yields a table schema like this:
create table Batch (
id numeric(19,0) identity not null,
acceptedDate datetime null,
acceptedUser numeric(19,0) null,
checkedDate datetime null,
checkedUser numeric(19,0) null,
createdDate datetime null,
createdUser numeric(19,0) null,
holdDate datetime null,
holdUser numeric(19,0) null,
modifiedDate datetime null,
modifiedUser numeric(19,0) null,
openedDate datetime null,
openedUser numeric(19,0) null,
postedDate datetime null,
postedUser numeric(19,0) null,
receivedDate datetime null,
receivedUser numeric(19,0) null,
primary key (id)
);

Hope this helps.


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