Hibernate Config / war

Discussion in 'Java' started by Daniel, Mar 8, 2005.

  1. Daniel

    Daniel Guest

    Hi all.

    Currently, I have Hibernate working in an exploded directory.

    I am having a problem setting up Hibernate's configuration when it is
    wrapped up in a war file. The problem, I believe, is somewhere in
    setting up the Configuration object. Can you send me or post some code
    that shows me how to get the hibernate.cfg.xml file from the WAR and
    how you configured Configuration?

    I tried getClass().getResourceAsStream( path ) and then converted that
    stream into a String, and tried Configuration.configure( string ). It
    found the file and I printed it out the contents of that string to make
    sure and I do see the hibernate.cfg.xml contents.

    I put the war in Tomcat 5's webapp dir and got the following during
    startup:

    "net.sf.hibernate.HibernateException: (The contents of my
    hibernate.cfg.xml go here) not found"



    Second related question, when developing I would prefer to use the
    exploded directory format, so would I have to have one code for getting
    the config from the war file and one code for getting it from a
    java.io.File, and then comment one of those codes out depending if I am
    developing or want to test it as a war file?

    Much appreciated and thanks in advance.
     
    Daniel, Mar 8, 2005
    #1
    1. Advertising

  2. Daniel

    Oscar kind Guest

    Daniel <> wrote:
    > I am having a problem setting up Hibernate's configuration when it is
    > wrapped up in a war file. The problem, I believe, is somewhere in
    > setting up the Configuration object. Can you send me or post some code
    > that shows me how to get the hibernate.cfg.xml file from the WAR and
    > how you configured Configuration?
    >
    > I tried getClass().getResourceAsStream( path ) and then converted that
    > stream into a String, and tried Configuration.configure( string ). It
    > found the file and I printed it out the contents of that string to make
    > sure and I do see the hibernate.cfg.xml contents.
    >
    > I put the war in Tomcat 5's webapp dir and got the following during
    > startup:
    >
    > "net.sf.hibernate.HibernateException: (The contents of my
    > hibernate.cfg.xml go here) not found"


    Did you put the configuration file in the webapps directory with the
    directories and war files for the wep applications, in WEB-INF, or in
    WEB-INGF/classes? Only the last one is found using the classloader (as you
    otherwise did cortrectly).


    > Second related question, when developing I would prefer to use the
    > exploded directory format, so would I have to have one code for getting
    > the config from the war file and one code for getting it from a
    > java.io.File, and then comment one of those codes out depending if I am
    > developing or want to test it as a war file?


    No, one piece of code is sufficient: In the exploded form, you can also
    use Class#getResourceAsStream(String)


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
     
    Oscar kind, Mar 8, 2005
    #2
    1. Advertising

  3. Daniel

    Daniel Guest

    Hi Oscar,

    I have one single war file. Inside the war file, I have
    WEB-INF/classes/hibernate/hibernate.cfg.xml. When I load the xml file,
    I use getClass().getResourceAsStream( "/hibernate/hibernate.cfg.xml" ).


    Then, I put this war file into Tomcat's webapps directory. The only
    hibernate.cfg.xml file is inside the war.

    So:

    c:\java\tomcat\webapps\myapp.war

    Inside the myapp.war:

    WEB-INF\classes\hibernate\hibernate.cfg.xml
     
    Daniel, Mar 8, 2005
    #3
  4. Daniel

    Guest

    Your hibernate.cfg.xml file should reside in
    htdocs/WEB-INF/classes

    And your *.hbm.xml files should reside in the
    same directory as your class files.
     
    , Mar 8, 2005
    #4
  5. Daniel

    Daniel Guest

    Hi,

    Isn't htdocs part of apache and not tomcat?

    I am using tomcat...

    Thanks.

    wrote:
    > Your hibernate.cfg.xml file should reside in
    > htdocs/WEB-INF/classes
    >
    > And your *.hbm.xml files should reside in the
    > same directory as your class files.
     
    Daniel, Mar 9, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    0
    Views:
    1,758
    Ilias Lazaridis
    Dec 27, 2004
  2. Replies:
    0
    Views:
    3,772
  3. jstorta
    Replies:
    1
    Views:
    11,831
  4. Eleanna Skouta
    Replies:
    2
    Views:
    8,486
    Arne Vajhøj
    Dec 9, 2008
  5. Amit Jain
    Replies:
    7
    Views:
    3,363
    Amit Jain
    Apr 27, 2009
Loading...

Share This Page