microsoft adobe reader download

adobe acrobat x buy cheap free download adobe 6 adobe illustrator cs3 keygen download buy cheap free download adobe photoshop adobe acrobat full download

latest adobe download

adobe creative suite cheapest free adobe pdf download adobe 9 download buy cheap download adobe reader for mac os 10 dreamweaver 8 download adobe

free adobe writer download

creative suite 5 cheapest free download adobe acrobat installer adobe golive download cheapest adobe cs2 download free crack key generator free adobe photoshop software download

mac adobe reader download

buy cheap premiere pro cs5 adobe in design 2 download adobe premier plugins download cheapest adobe distiller download adobe photoshop elements 5 free download

free download adobe photoshop cs

buy cheap Adobe Flash CS5.5/a> adobe cs2 download crack key generator download adobe premiere elements cheapest adobe pagemaker full download adobe acrobat reader 9 download

my space adobe flash player download

in copy adobe cs6 buy cheap download adobe photoshop elements free adobe illustrator cs2 download buy cheap download adobe flashplayer free adobe robohelp 7 download

free adobe photoshop download cs2

cheapest indesign cs6 download adobe audition 2 for free free adobe photoshop full version download cheap adobe slovar download adobe imageready download

adobe macromedia flash download

Dreamweaver CS6 cheapest download adobe acrobat professional english adobe 8 reader download buy cheap free adobe dream weaver 8 download adobe raider 8 download

adobe bridge download

cheapest InCopy CS6 for mac adobe photoshop 2007 free download adobe acrobat flash download cheapest adobe acrobat reader 9 download adobe 8 reader download

download from adobe flash player

Photoshop CS6 mac cheap where can i download adobe flash player 9 adobe acrobat pro download cheapest adobe free download adobe cs3 mac download

adobe flash free download

Premiere Pro CS6 cheap free download adobe flash player download free adobe acrobat cheap adobe gamma download adobe 10 download

adobe premiere tryout download

creative suite 5 discount adobe pdf reader free download free adobe reader 8 to download buy cheap download adobe photoshop 7 download adobe scanner

old adobe software download

adobe incopy cheap free adobe photoshop full version download adobe indesign trial download buy cheap adobe page maker full download download adobe acrobat 7

adobe premier isxmpeg codec download

adobe creative suite 5 discount download isxmpeg codec from adobe premier free adobe mobile phone download cheap free download adobe illustrator cs3 free download adobe acrobat reader professional 6 cracked

download adobe flash player pictures

photoshop lightroom 3 cheapest how to download adobe flash videos free download hustler rmagizne in adobe format cheap download adobe elements download adobe reader for macintosh

free download adobe photo shop

buy cheap cs5 master collection adobe download download adobe reader version 5 discount download adobe photoshop 50 adobe photoshop cs3 download

adobe acrobat reader 4 download

discount adobe premiere pro adobe photoshop cs 3 download adobe pdf download security cheapest free download of adobe reader adobe photoshop 70 download

free adobe photoshop 7 brushes download

adobe web premium buy cheap adobe 6 download download adobe 5 buy cheap adobe rider download adobe flash player 8 free download

ColdFusion-ORM: Define One-to-Many and Many-to-one relationships


Task:
Example to demonstrate how to establish one-to-many and many-to-one Relationships in ColdFusion-ORM

Previous Related Posts:
Getting Started with ORM
ColdFusion-ORM: Using CRUD Functions

Stuff that you would learn:
-  how to establish a One-to-Many relationship between 2 CFCs.
-  how to establish a Many-to-One relationship between 2 CFCs.
-  how to do CRUD operations with CFCs which have a relationship.
-  how to use the implicit relationship methods added by ColdFusion.
-  cascade insert and delete

Steps to Run the example:
-  This example needs the cfartgallery datasource. This is shipped with ColdFusion by default.
-  Create a directory say "1tonorm" under webroot.
-  Create the following files – Application.cfc, Art.cfc, Artists.cfc and index.cfm.
-  Run the URL http://localhost:8500/1tonorm/index.cfm

I have interspersed the example with a lot of comments. You can understand the concept by just following the comments starting with Application.cfc and then Artists.cfc and then Arts.cfc and then index.cfm.

Application.cfc

component
{
    //Name of the application
    this.name = "ORM_One2Many";

    //ormenabled should be set to true so that ORM is enabled for this application
    this.ormenabled = "true";

    //Set the datasource that needs to be used by the ORM Functions.
    this.datasource = "cfartgallery";
}

Artists.cfc

component persistent="true"
{
    property name="artistid" generator="increment";
    property firstname;
    property lastname;
    property address;
    property city;
    property state;
    property postalcode;
    property email;
    property phone;
    property fax;
    property thepassword;

    /*
    Artists have many arts and henace they form a one-to-many relation. The idea is to have all
    the arts as an array of art objects in each Artists' object.

    Set fieldytpe="one-to-many".

    Specify CFC="Art" to convey that the relationship is with Art.cfc.

    fkcolumn="artistid" to specify the foreign key.  But this is not required if foreignkey constraint
    is defined in the database - ColdFusion-ORM will figure it out by inspecting the database.

    cascade="all-delete-orphan": "all" signifies that all the CRUD operations will be cascaded to the
    related objects. "delete-orphan" means that it will delete the orphan objects in the art array
    i.e. if i remove an art from the arts array, it is now an orphan as it has no artist associated
    with it and hence will be automatically deleted.

    Implit Methods:    When a one-to-many relationship is established, the following implicit methods are introduced
    into the artist object:
    void addart(<artobj>) - used to add a art object to the relationship.
    boolean removeart(<artobj>) - used to remove the art object from the relationship.
    boolean hasart() - to check if there are any art objects in the artist object.
    boolean hasart(<artobj>) - to check if the input art object is present in the artist object.

    singularname: Use this attribute to specify custom name to the above implicit methods.

    type=array: To retrieve the arts objects as an array.  If type="struct", then the arts objects are retrieved as
    key-value pairs.  structkeycolumn and structkeytype should be specified in addition to all the above attributes
    to retrieve arts as key-value pairs.

    inverse=true: In a bi-directional relationship like this, you should specify one of the side as the controlling side
    which will set the relationship.  Usually, in a one-to-many bi-directional relationship, the many-to-one side should
    be the controlling side.  To do that, set inverse=true on this side (one-to-many side).
    */
    property name="arts" type="array" fieldtype="one-to-many" cfc="Art" singularname="art" fkcolumn="artistid"
    inverse="true" cascade="all-delete-orphan";
}

Art.cfc

component persistent="true"
{
    property name="artid" generator="increment";
    property name="artname";
    property name="price";
    property name="largeimage";
    property name="mediaid";
    property name="issold";  

/*
  Many arts have an artist and hence form a "many-to-one" relation.

  To establish a "many-to-one" relation, set fieldytpe="many-to-one"

  Specify CFC="Artists" to convey that the relationship is with Artists.cfc.

  fkcolumn="artistid": Used to specify the foreign key.

  missingRowIgnored: If the value is true, and the row that is referenced by the foreign
  key is missing, it is treated as a null association.
*/
    property name="artist" fieldtype="many-to-one" fkcolumn="artistid"
    cfc="Artists" missingRowIgnored="true";
}

index.cfm

<!---
This example will teach you
- how to establish a One-to-Many relationship between 2 CFCs.
- how to establish a Many-to-One relationship between 2 CFCs.
- how to do CRUD operations with CFCs which have a relationship.
- how to use the implicit relationship methods added by ColdFusion.
- Using cascade in one-to-many to many-to-one relationships.

cfartgallery datasource is used for this application.
--->
<cfscript>
    ormreload();

    /*
        Display the existing records in Artists Table.
        This will display the artist and their arts.
    */
    WriteOutput("<b>Initial state of the Artists and Art tables<br></b>");
    DisplayArtists();

    /*
        Create an artist object and 2 art objects
    */
    newArtist = new Artists();
    newArtist.setfirstname("Daniel");
    newArtist.setlastname("Richard");

    newArt1 = new Art();
    newArt1.setArtName("Champions");

    newArt2 = new Art();
    newArt2.setArtName("Isolate");

    /*
        Associate the Arts to the Artist.  Notice that the
        implicit-relationship method - addArt is called.
    */
    newArtist.addArt(newArt1);
    newartist.addArt(newArt2);

    /*
        The one-to-many relationship should be established from both
        the ends.  Hence set the Artist to the new Arts.
    */
    newArt1.setArtist(newArtist);
    newArt2.setArtist(newArtist);

    /*
        Save the new artist. Note that EntitySave will save the artist as
        well as the associated arts as well (cascade-insert)
    */
    EntitySave(newArtist);
    ormflush();

    /*
        Display the existing records to check whether insert succeeded.
    */
    WriteOutput("<b>State of the Artists and Art tables after insert.  Notice that the new
                artist - Daniel Richard is added with his arts 'Champions' and 'Isolate'<br></b>");
    DisplayArtists();

    /*
        Remove newArt2 and add newArt3.  In a way, you are just modifying the 'arts' array.  Notice that
        we are using the implicit=relationship method - removeArt is called.
    */
    newArt3 = new Art();
    newArt3.setArtName("Holiday");
    newArt3.setArtist(newArtist);
    newArtist.addArt(newArt3);
    newArtist.removeArt(newArt2);
    ormflush();    

    /*
        Check if delete and update of art succeeded.
    */
    WriteOutput("<b>State of the Artists and Art tables after Deleting/Updating his Arts.  Notice that
                    the art 'Isolate' is removed and the art 'Holiday' is added.<br></b>");
    DisplayArtists();

    /*
        Delete the Artist.  Note that EntityDelete will delete
        the Artist as well as the associated arts. (cascade-delete).
    */
    EntityDelete(newArtist);
    ormflush();

    /*
        Check if delete succeeded.
    */
    WriteOutput("<b>State of the Artists and Art tables after deleting the Artist.
                Notice that the artist 'Daniel Richard' is deleted.  All his arts are also deleted!<br></b>");
    DisplayArtists();

    /*
        A Utility function to display artists and his arts
    */
    function DisplayArtists()
    {
        query1 = new Query();
        query1.setSQL("select ArtistID, firstname, lastname from Artists where ArtistID>10");
        WriteDump(query1.execute().getresult());

        query1.setSQL("select ArtID, ArtName, ArtistID from Art where ArtID>50");
        WriteDump(query1.execute().getresult());
    }
</cfscript>

del.icio.us Tags: ,,




Comments



1
Author:  John Whish | Date:  September 20, 2009 | Time:  2:10 AM

Hi Manju. Great series of posts! I have a question. In your index.cfm file you say that “The one-to-many relationship should be established from both the ends.” Why do you need to this? If you associate from one side, doesn’t hibernate know about the relationship?

2
Author:  Manjukiran | Date:  October 9, 2009 | Time:  6:14 PM

Hi John, hibernate would know it. But, the calls newArt1.setArtist(newArtist) and newArt2.setArtist(newArtist); wouldn’t be automatic. The newArt1 and newArt2 objects would continue to have Artist=NULL and would be out-of-sync with the db.

3
Author:  Neil Moncur | Date:  October 29, 2009 | Time:  8:16 PM

Manju, I really appreciate this post. The livedocs are great, but their examples are very simplistic, and it is difficult to get the full picture from them. Having a complete example here is very helpful.

4
Author:  Justin Carter | Date:  October 30, 2009 | Time:  8:10 PM

Nice example usage :) One question though…

Is there any reason why you are calling DisplayArtists like this:
DisplayArtists(EntityLoad(”Artists”));
when the DisplayArtists function itself doesn’t accept or use any arguments? Shouldn’t you just be calling DisplayArtists() each time instead?

5
Author:  Manjukiran | Date:  October 30, 2009 | Time:  8:28 PM

@Neil, thanks for the pat!

@Justin, good catch. DisplayArtists initially used the objects to display the data but later i had changed it to fetch data using SQL – missed to update the calls. Will correct it now :)

6
Author:  Don | Date:  May 28, 2010 | Time:  11:51 PM

This is interesting but basic. Do you have a tutorial or know of one showing how to do something like say a pivot table?
For instance say if you extend this out so that you have art galleries and you want to have a table that connects artists to galleries. Each artist could have art in more than 1 gallery. Thus the pivot table.

I have this exact scenario but with volunteers. They could volunteer with more than one organization so I have a table that keeps track of volunteer-organization.

How would I do that with ORM?

7
Author:  Tom | Date:  July 6, 2010 | Time:  2:03 PM

For me the example doesn’t work, the Art records are never added.
Maybe something changed in the ColdFusion version 9,0,0,251028 of ORM?

I’ll have a look on the Adobe website.

Otherwise I have been enjoying your tutorials, they are very helpfull! Thanks!

8
Author:  Tom | Date:  July 6, 2010 | Time:  2:53 PM

Ok remove my post please, seems i had a case of cache.
I was lazy and didn’t create a new directory, rather i just overwrite the files from previous tutorials.

The tutorial is working fine!

9
Author:  Alex | Date:  September 23, 2010 | Time:  1:06 AM

I had the same caching issue – made a new directory and everything is good. What is being cached though?

10
Author:  Sanoop | Date:  January 10, 2011 | Time:  8:33 PM

Hi Manju. great post, lot of new thing to learn.

But I am getting below error while executing the project (ORM1)-
Id field not defined for cfc ORM1.Art. Either the table should have a PK column mapped to a cfc field OR at least one field should be specified as id.

Seems DB related error :( not so good in DB
Below are the database scripts –

CREATE TABLE ARTISTS
(
ARTISTID NUMBER(4) NOT NULL,
FIRSTNAME VARCHAR2(20 BYTE),
LASTNAME VARCHAR2(20 BYTE),
ADDRESS VARCHAR2(20 BYTE),
CITY VARCHAR2(20 BYTE),
STATE VARCHAR2(20 BYTE),
postalcode VARCHAR2(20 BYTE),
email VARCHAR2(20 BYTE),
phone NUMBER(4)
)

ALTER TABLE ARTISTS ADD (
CONSTRAINT ARTIST_PK
PRIMARY KEY
(ARTISTID));
=======================AND==================
CREATE TABLE ART
(
ARTISTID NUMBER(4) NOT NULL,
ARTID NUMBER(4),
ARTNAME VARCHAR(20),
PRICE NUMBER(4),
ISSOLD VARCHAR(4)
);

ALTER TABLE ART ADD (
CONSTRAINT ARTIST_FK
FOREIGN KEY (ARTISTID)
REFERENCES ARTISTS (ARTISTID));

-> I am really got stuck here any help.

11
Author:  Sanoop | Date:  January 18, 2011 | Time:  8:09 PM

Hi,

Some how I am able to see all the records under Artists and Art in One to Many Relationship, but when I try to Associate the Arts to the Artist. and try to set the Artist to the new Arts.

I encountered with errors – below is the stack trace

ids for this class must be manually assigned before calling save(): Art
Root cause :o rg.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): Art

The error occurred in C:\Inetpub\wwwroot\ORM1\index.cfm: line 66

64 : art2.setArtists(artists1);
65 :
66 : ormflush();
67 :
68 : } catch(Exception ex){

—-

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): Art
at org.hibernate.id.Assigned.generate(Assigned.java:56)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:534)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:526)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:241)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:291)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:239)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:192)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:319)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:265)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:192)
at org.hibernate.engine.Cascade.cascade(Cascade.java:153)

I show earlier comment from Tom saying that It might not work with CF9.0.

Any suggestion pls
Thanks,
Sanoop

12
Author:  google | Date:  April 27, 2011 | Time:  7:03 PM

You complete me.You possessed me at Hello, you possessed me at Hello.

13
Author:  Thomas Craig | Date:  June 1, 2011 | Time:  6:04 PM

This is a great explanation on the orm relationship topic, much better then what adobe has up on their site. thanks.

14
Author:  James Lamar | Date:  June 21, 2011 | Time:  10:39 AM

2 years later and still one of the best ORM examples I have seen yet.

Thanks!

15
Author:  Jeremy Crane | Date:  July 28, 2011 | Time:  4:04 AM

I posted a question on flexcoders that is related to a problem I’m having using your example in an Offline AIR application. I’d appreciate it if you could take a look to see if you might have a solution to my problem. Thanks. http://tech.groups.yahoo.com/group/flexcoders/message/163229

16
Author:  Did it cross you mind that you will be able to save money while smoking | Date:  September 10, 2011 | Time:  10:52 PM

There are definitely a number of particulars like that to take into consideration. That may be a great point to carry up. I offer the thoughts above as general inspiration but clearly there are questions just like the one you bring up where the most important factor shall be working in honest good faith. I don?t know if finest practices have emerged round issues like that, however I’m sure that your job is clearly recognized as a good game. Each girls and boys really feel the impression of only a moment’s pleasure, for the rest of their lives.

17
Author:  read this | Date:  February 29, 2012 | Time:  8:47 AM

WOW just what I was looking for. Came here by searching for supplements

18
Author:  Shiloh Brasswell | Date:  March 16, 2012 | Time:  9:40 AM

you are actually a just right webmaster. The web site loading pace is incredible. It seems that you are doing any distinctive trick. In addition, The contents are masterpiece. you’ve performed a wonderful process on this topic! Elegant Models, 68,Lombard Street, London, EC3V 9LJ, 020 3011 2893

19
Author:  Jasmineea | Date:  March 1, 2013 | Time:  2:17 PM

(this.value==

20
Author:  crplsa.info | Date:  March 8, 2013 | Time:  1:58 PM

Ahaa, its good discussion on the topic of this piece
of writing here at this webpage, I have read all that,
so at this time me also commenting here.

21
Author:  penis extension | Date:  May 20, 2013 | Time:  10:50 AM

Wow, amazing blog layout! How lengthy have you been running a
blog for? you make blogging glance easy. The full look of your site is wonderful, as well as
the content material!



Write a Comment

Note: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>