-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate newbie
PostPosted: Thu Nov 23, 2006 4:07 pm 
Newbie

Joined: Thu Nov 23, 2006 3:29 pm
Posts: 5
Location: Venezuela
Hello. I am trying to make my first composite-id mapping file and I get an InvalidMappingException when i call the configure method in the Configure class.

I have 2 tables: Stock and StockPrice, here is the definition in postgresql:

Code:
CREATE TABLE stock
(
  id serial NOT NULL,
  symbol varchar(20) NOT NULL,
  CONSTRAINT pk_stock PRIMARY KEY (id)
)

CREATE TABLE stockprice
(
  date date NOT NULL,
  stockid int4 NOT NULL,
  price numeric(12,5) NOT NULL,
  CONSTRAINT pk_stockprice PRIMARY KEY (date, stockid),
  CONSTRAINT fk_stockprice__stock FOREIGN KEY (stockid)
      REFERENCES stock (id) MATCH SIMPLE
      ON UPDATE RESTRICT ON DELETE RESTRICT
)

I created the Stock, StockPrice and StockPricePK classes with a default constructor, the getters and setters methods, override equals and hashCode and StockPrice implements Serializable. Here is the mapping files for both classes:

Stock.hbm.xml:
Code:
<hibernate-mapping>
    <class name="Stock" table="stock">
        <id name="_id" column="id">
            <generator class="sequence">stock_id_seq</generator>
        </id>
        <property name="_symbol" column="symbol" not-null="true" unique="true" />
        <map name="_prices" lazy="true">
            <key column="date" />
            <one-to-many column="stockid" class="domain.StockPrice" />
        </map>
    </class>
</hibernate-mapping>


StockPrice.hbm.xml:
Code:
<hibernate-mapping>
    <class name="StockPrice" table="stockprice">
        <composite-id class="domain.StockPricePK">
            <key-property name="_date" column="date"/>
            <key-many-to-one name="_stockId" column="stockid" class="domain.Stock"/>
        </composite-id>
        <property name="_price" column="price" not-null="true" />
    </class>
</hibernate-mapping>


This is the code i am using to create the session factory:

Code:
public static SessionFactory getSessionFactory()
    {
        if(_sessionFactory == null)
        {
            Configuration configuration = new Configuration().configure();
            _sessionFactory = configuration.buildSessionFactory();
        }
       
        return _sessionFactory;
    }

This is the line where the exceptino is throwed:
Configuration configuration = new Configuration().configure();

The exception stacktrace is the following:

org.hibernate.InvalidMappingException: Could not parse mapping document from resource domain/StockPrice.hbm.xml
org.hibernate.cfg.Configuration.addResource(Configuration.java:523)
org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1511)
org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1479)
org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1458)
org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1432)
org.hibernate.cfg.Configuration.configure(Configuration.java:1352)
org.hibernate.cfg.Configuration.configure(Configuration.java:1338)
helper.HibernateHelper.getSessionFactory(HibernateHelper.java:21)
servlet.TestServlet.doGet(TestServlet.java:29)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


I am using jdk 1.4.2 and Hibernate 3 with postgresql 8.0. Here is the hibernate.cfg.xml:
Code:
<hibernate-configuration>
    <session-factory>
        <property name="connection.datasource">java:/comp/env/jdbc/hibernateDB</property>
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="show_sql">false</property>
        <mapping resource="domain/StockPrice.hbm.xml" />
        <mapping resource="domain/Stock.hbm.xml" />       
    </session-factory>
</hibernate-configuration>

I hope you can help me.

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 5:58 pm 
Beginner
Beginner

Joined: Thu Oct 12, 2006 6:19 pm
Posts: 34
Location: Guatemala
Hi carlos, could you post the entire error trace throwed by your application, specifically the line saying you what is wrong in your resource domain/StockPrice.hbm.xml. I don't see any error in it, but it's sure something is bad.

Thanks

_________________
God is Love


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 7:12 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
In which package is your StockPrice located ? Seems to be domain.

I guess you should specify Fully qualified name for your class in your xml. Should be something like :
Code:
<class name="domain.StockPrice" table="stockprice">

instead of what you put :
Code:
<class name="StockPrice" table="stockprice">

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 11:41 am 
Newbie

Joined: Thu Nov 23, 2006 3:29 pm
Posts: 5
Location: Venezuela
Here is the exception stacktrace:

GRAVE: Error parsing XML: XML InputStream(14) The content of element type "map" must match "(meta*,subselect?,cache?,synchronize*,comment?,key,(map-key|composite-map-key|map-key-many-to-many|index|composite-index|index-many-to-many|index-many-to-any),(element|one-to-many|many-to-many|composite-element|many-to-any),loader?,sql-insert?,sql-update?,sql-delete?,sql-delete-all?,filter*)".
org.hibernate.InvalidMappingException: Could not parse mapping document from resource domain/Stock.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:523)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1511)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1479)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1458)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1432)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1352)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1338)
at helper.HibernateHelper.getSessionFactory(HibernateHelper.java:23)
at servlet.TestServlet.doGet(TestServlet.java:29)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: org.hibernate.MappingException: invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:463)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:520)
... 27 more
Caused by: org.xml.sax.SAXParseException: The content of element type "generator" must match "(param)*".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:172)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:382)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleEndElement(XMLDTDValidator.java:2048)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(XMLDTDValidator.java:932)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanEndElement(XMLNSDocumentScannerImpl.java:719)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1685)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:460)
... 28 more

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 12:25 pm 
Newbie

Joined: Mon Aug 02, 2004 1:21 pm
Posts: 9
The problem is the generator element. It should match the following structure:

Code:
<id name="id" type="long" column="cat_id">
        <generator class="org.hibernate.id.TableHiLoGenerator">
                <param name="table">uid_table</param>
                <param name="column">next_hi_value_column</param>
        </generator>
</id>


Regards,

_________________
Camilo A.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 12:43 pm 
Newbie

Joined: Thu Nov 23, 2006 3:29 pm
Posts: 5
Location: Venezuela
Thanks camilosaurio. It was one of the problema. But the exception stills appears. Now the problem is with the Map mapping. The Stock.hbm.xml file is now:

Code:
<hibernate-mapping>
    <class name="domain.Stock" table="stock">
        <id name="_id" column="id">
            <generator class="sequence">
                <param name="sequence">stock_id_seq</param>
            </generator>
        </id>       
        <map name="_prices" lazy="true">
            <key column="date" />
            <one-to-many column="stockid" class="domain.StockPrice" />
        </map>
        <property name="_simbol" column="simbol" not-null="true" unique="true" />
    </class>
</hibernate-mapping>


And the error the log shows is:
INFO: Reading mappings from resource: domain/Stock.hbm.xml
24-nov-2006 12:34:47 org.hibernate.util.XMLHelper$ErrorLogger error
GRAVE: Error parsing XML: XML InputStream(14) Attribute "column" must be declared for element type "one-to-many".
24-nov-2006 12:34:47 org.hibernate.util.XMLHelper$ErrorLogger error
GRAVE: Error parsing XML: XML InputStream(15) The content of element type "map" must match "(meta*,subselect?,cache?,synchronize*,comment?,key,(map-key|composite-map-key|map-key-many-to-many|index|composite-index|index-many-to-many|index-many-to-any),(element|one-to-many|many-to-many|composite-element|many-to-any),loader?,sql-insert?,sql-update?,sql-delete?,sql-delete-all?,filter*)".

The line 14 is:
Code:
<one-to-many column="stockid" class="domain.StockPrice" />


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 1:02 pm 
Newbie

Joined: Mon Aug 02, 2004 1:21 pm
Posts: 9
It seems that you are missing the map-key element

Code:
<map
    name="propertyName"                                         (1)
    table="table_name"                                          (2)
    schema="schema_name"                                        (3)
    lazy="true|extra|false"                                     (4)
    inverse="true|false"                                        (5)
    cascade="all|none|save-update|delete|all-delete-orphan|delet(6)e-orphan"
    sort="unsorted|natural|comparatorClass"                     (7)
    order-by="column_name asc|desc"                             (8)
    where="arbitrary sql where condition"                       (9)
    fetch="join|select|subselect"                               (10)
    batch-size="N"                                              (11)
    access="field|property|ClassName"                           (12)
    optimistic-lock="true|false"                                (13)
    mutable="true|false"                                        (14)
    node="element-name|."
    embed-xml="true|false"
>

    <key .... />
    <map-key .... />
    <element .... />
</map>


regards,

_________________
Camilo A.


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