Dada44
asked on
Understanding CMR
Hi,
I've been looking for a site or a book where to learn CMR but I'm getting crazy, all the examples are too long and complicated for me to understand.
Why do I need CMR?
Well, I have two tables in my database: Tickets and Concerts. If I want to calculate the number of tickets demanded for one concert .. I suppose I need to relate Tickets and Concerts beans .. but how? What fields should I create in the tables?
I'm starting to use jBuilder and creating the relationship, once I understand what fields I have to use, is not a problem but what is a mystery to me is: after creating the fields in database and the relationship in between the beans, what's next? which is the code I have to use in the servlet in order to manage the relationship? And what about the transactions? How are they used within CMR?
Thanks a million in advance
I've been looking for a site or a book where to learn CMR but I'm getting crazy, all the examples are too long and complicated for me to understand.
Why do I need CMR?
Well, I have two tables in my database: Tickets and Concerts. If I want to calculate the number of tickets demanded for one concert .. I suppose I need to relate Tickets and Concerts beans .. but how? What fields should I create in the tables?
I'm starting to use jBuilder and creating the relationship, once I understand what fields I have to use, is not a problem but what is a mystery to me is: after creating the fields in database and the relationship in between the beans, what's next? which is the code I have to use in the servlet in order to manage the relationship? And what about the transactions? How are they used within CMR?
Thanks a million in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.oracle.com/technology/tech/java/oc4j/htdocs/how-to-cmr.html
ASKER
Thanks a lot. I’ve been studying the examples and I’ve created an example myself in order to apply it .. everything seems to be ok but I get an “ejb not bound” error all the time, no matter what I change.
Maybe you can help me, the example I’ve written is about to get all books from a book category (Fiction for example)
This is the jsp:
<%@page import="java.util.*,javax. transactio n.*,javax. naming.*, folder.*" %>
<%
Context ctx = new InitialContext();
CategoryHome ch = (CategoryHome) ctx.lookup("ejb/Category") ;
Category c = ch.findByPrimaryKey("Ficti on");
UserTransaction ut = (UserTransaction) ctx.lookup("java:comp/User Transactio n");
ut.begin();
Collection books = c.getBook();
%>
<b>Books within the category <%= c.getCategoryName() %></b><br>
<% Iterator it=books.iterator();
while(it.hasNext())
{
Book b=(Book)it.next();
%>
<%=b.getBookName()%>
<% }
ut.commit();
%>
JBOSS.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.2//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_2.dtd">
<jboss>
<enterprise-beans>
<entity>
<ejb-name>Book</ejb-name>
<jndi-name>Folder/BookRemo te</jndi-n ame>
<local-jndi-name>Book</loc al-jndi-na me>
</entity>
<entity>
<ejb-name>Category</ejb-na me>
<jndi-name>ejb/Category</j ndi-name>
<local-jndi-name>Category< /local-jnd i-name>
</entity>
</enterprise-beans>
</jboss>
EJB-JAR.XML (roughly)
<ejb-jar>
<relationships>
<ejb-relation>
<ejb-relation-name>book-ca tegory</ej b-relation -name>
<ejb-relationship-role>
<description>book</descrip tion>
<ejb-relationship-role-nam e>BookRela tionshipRo le</ejb-re lationship -role-name >
<multiplicity>Many</multip licity>
<relationship-role-source>
<description>book</descrip tion>
<ejb-name>Book</ejb-name>
</relationship-role-source >
<cmr-field>
<description>category</des cription>
<cmr-field-name>category</ cmr-field- name>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<description>category</des cription>
<ejb-relationship-role-nam e>Category Relationsh ipRole</ej b-relation ship-role- name>
<multiplicity>One</multipl icity>
<relationship-role-source>
<description>category</des cription>
<ejb-name>Category</ejb-na me>
</relationship-role-source >
<cmr-field>
<description>book</descrip tion>
<cmr-field-name>book</cmr- field-name >
<cmr-field-type>java.util. Collection </cmr-fiel d-type>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
</relationships>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Book</ejb-name>
<method-name>*</method-nam e>
</method>
<trans-attribute>Supports< /trans-att ribute>
</container-transaction>
<container-transaction>
<method>
<ejb-name>Category</ejb-na me>
<method-name>*</method-nam e>
</method>
<trans-attribute>Supports< /trans-att ribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
Maybe you can help me, the example I’ve written is about to get all books from a book category (Fiction for example)
This is the jsp:
<%@page import="java.util.*,javax.
<%
Context ctx = new InitialContext();
CategoryHome ch = (CategoryHome) ctx.lookup("ejb/Category")
Category c = ch.findByPrimaryKey("Ficti
UserTransaction ut = (UserTransaction) ctx.lookup("java:comp/User
ut.begin();
Collection books = c.getBook();
%>
<b>Books within the category <%= c.getCategoryName() %></b><br>
<% Iterator it=books.iterator();
while(it.hasNext())
{
Book b=(Book)it.next();
%>
<%=b.getBookName()%>
<% }
ut.commit();
%>
JBOSS.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.2//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_2.dtd">
<jboss>
<enterprise-beans>
<entity>
<ejb-name>Book</ejb-name>
<jndi-name>Folder/BookRemo
<local-jndi-name>Book</loc
</entity>
<entity>
<ejb-name>Category</ejb-na
<jndi-name>ejb/Category</j
<local-jndi-name>Category<
</entity>
</enterprise-beans>
</jboss>
EJB-JAR.XML (roughly)
<ejb-jar>
<relationships>
<ejb-relation>
<ejb-relation-name>book-ca
<ejb-relationship-role>
<description>book</descrip
<ejb-relationship-role-nam
<multiplicity>Many</multip
<relationship-role-source>
<description>book</descrip
<ejb-name>Book</ejb-name>
</relationship-role-source
<cmr-field>
<description>category</des
<cmr-field-name>category</
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<description>category</des
<ejb-relationship-role-nam
<multiplicity>One</multipl
<relationship-role-source>
<description>category</des
<ejb-name>Category</ejb-na
</relationship-role-source
<cmr-field>
<description>book</descrip
<cmr-field-name>book</cmr-
<cmr-field-type>java.util.
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
</relationships>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Book</ejb-name>
<method-name>*</method-nam
</method>
<trans-attribute>Supports<
</container-transaction>
<container-transaction>
<method>
<ejb-name>Category</ejb-na
<method-name>*</method-nam
</method>
<trans-attribute>Supports<
</container-transaction>
</assembly-descriptor>
</ejb-jar>
EJB is not my thing.
Maybe this can help: http://forum.java.sun.com/thread.jsp?forum=13&thread=414432&message=1830183
Maybe this can help: http://forum.java.sun.com/thread.jsp?forum=13&thread=414432&message=1830183