Проблема с hibernate

NNemo
Уже с Приветом
Posts: 1935
Joined: 15 Sep 2003 17:49
Location: Ukraine, Mariupol -> USA everywhere :-)

Проблема с hibernate

Post by NNemo »

Уперся как рогом в стенку.

Пытаюсь сгенерить классы с помощью ant и hbm2java

BUILD FAILED
E:\Apache\Projects\HibernateTutorial\build.xml:75: Caused by:
Caused by:
java.lang.NoClassDefFoundError: net/sf/hibernate/MappingException
at net.sf.hibernate.tool.hbm2java.Hbm2JavaTask.processFile(Hbm2JavaTask.
java:145)
at net.sf.hibernate.tool.hbm2java.Hbm2JavaTask.execute(Hbm2JavaTask.java
:95)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)

Что это означает? Что класс не найден. Или ошибка в синтаксисе маппинга.
Yuri Khupchenko
Новичок
Posts: 37
Joined: 27 Apr 2004 15:25
Location: SPb

Re: Проблема с hibernate

Post by Yuri Khupchenko »

NNemo wrote:Что это означает? Что класс не найден. Или ошибка в синтаксисе маппинга.


Скорее всего, в build.xml некорректно указан CLASSPATH для Hibernate. У меня работает связка XDoclet+Ant+Hibernate - никаких проблем. В случае чего, могу выслать примеры build.xml и структуры каталогов.
NNemo
Уже с Приветом
Posts: 1935
Joined: 15 Sep 2003 17:49
Location: Ukraine, Mariupol -> USA everywhere :-)

Post by NNemo »

What must be there?
My classpath contains hibernate2.jar and hibernate-tools.jar.
What's more?

As i can judge from class name it must be somewhere in these packages.
:pain1:
User avatar
donguz
Уже с Приветом
Posts: 856
Joined: 16 Sep 2002 16:13
Location: The Hub

Post by donguz »

NNemo wrote:What must be there?
My classpath contains hibernate2.jar and hibernate-tools.jar.
What's more?

As i can judge from class name it must be somewhere in these packages.
:pain1:


У меня в build.xml к Hibernate прописаны следующий файлы:
hibernate2.jar
cglib-2.0-rc2.jar
commons-collections-2.1.jar
dom4j-1.4.jar
ehcache-0.6.jar
jta.jar
odmg-3.0.jar
Yuri Khupchenko
Новичок
Posts: 37
Joined: 27 Apr 2004 15:25
Location: SPb

Post by Yuri Khupchenko »

В build.xml должно быть что-то вроде:

Code: Select all

    <property name="build.dir"   location="build"/>

    <path id="classpath.build">
        <fileset dir="lib"/>
    </path>

    <path id="classpath.runtime">
        <fileset dir="lib"/>
        <pathelement location="build"/>
    </path>

    <target name="java" description="Compile Java">
        <mkdir dir="${build.dir}"/>
        <javac srcdir="${src.java}"
               destdir="${build.dir}"
               classpathref="classpath.build"
               debug="true"/>
        <copy todir="${build.dir}">
            <fileset dir="${src.java}">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>


Причин может быть всего две:
1) ant не может определить taskdef;
2) неверный CLASSPATH для target.

Здесь все очень просто: если файлы, которые указал donguz, лежат в lib, все должно быть ОК.

Хотелось бы увидеть build.xml и узнать, что где лежит.
NNemo
Уже с Приветом
Posts: 1935
Joined: 15 Sep 2003 17:49
Location: Ukraine, Mariupol -> USA everywhere :-)

Post by NNemo »

Ok!
I got it.

Don't wanna show my files! :oops: Just started to study all that stuff.

One more question for you, guys?

Is it possible to do optimistic locking and reconciliation in hibernate?

I have pretty complex document structure with master details. What i would like to do is provide user with ability edit whole document, submit it and if something changed in underlying db, show changed parts, new one and keep user edited values as well.
Lets pretend i have an invoise with items. User edited several items and submit document. Mean time another user changed some of these items in background. I would like not only get update error, but give user ability to work around it.
Do youn understand what i mean?

I'm going to use timestamp in MS SQL or sequence in Oracle for optimistic locking.

Is that doable?
NNemo
Уже с Приветом
Posts: 1935
Joined: 15 Sep 2003 17:49
Location: Ukraine, Mariupol -> USA everywhere :-)

Post by NNemo »

Damn it!

Generated files is empty!
Yuri Khupchenko
Новичок
Posts: 37
Joined: 27 Apr 2004 15:25
Location: SPb

Post by Yuri Khupchenko »

NNemo wrote:Ok!
I got it.

Don't wanna show my files! :oops: Just started to study all that stuff.


Шпиен однако, не иначе :wink:

NNemo wrote:Is it possible to do optimistic locking and reconciliation in hibernate?


Это запросто:

Code: Select all

public class Foo{

    private int version;

    void setVersion(int version) {
        this.version = version;
    }
    int getVersion() {
        return version;
   }
}


Mapping file:

Code: Select all

<class name="Foo" table="FOO">
    <id ...></id>
    <version name="version" column="VERSION"/>
   
</class>


Или по-другому:

Code: Select all

public class Foo{
   
    private Date timeStamp;
   
    void setTimeStamp(Date timeStamp) {
        this.timeStamp = timeStamp;
    }

    public Date getTimeStamp() {
        return timeStamp;
    }
}


Mapping file:

Code: Select all

<class name="Foo" table="FOO">
    <id ...></id>
    <timestamp name="timeStamp" column="TIME_STAMP"/>

</class>


Единственное правило: тэги version и timestamp должны следовать сразу после тэга id
NNemo
Уже с Приветом
Posts: 1935
Joined: 15 Sep 2003 17:49
Location: Ukraine, Mariupol -> USA everywhere :-)

Post by NNemo »

That gona kill me...

codegen:
[hbm2java] Processing 1 files.
[hbm2java] Building hibernate objects
[hbm2java] log4j:WARN No appenders could be found for logger (net.sf.hibernate.
util.DTDEntityResolver).
[hbm2java] log4j:WARN Please initialize the log4j system properly.
[hbm2java] java.lang.NullPointerException
[hbm2java] at net.sf.hibernate.tool.hbm2java.BasicRenderer.isPropertySet(Ba
sicRenderer.java:611)
[hbm2java] at net.sf.hibernate.tool.hbm2java.BasicRenderer.generateConcrete
EmptyClasses(BasicRenderer.java:332)
[hbm2java] at net.sf.hibernate.tool.hbm2java.BasicRenderer.render(BasicRend
erer.java:59)
[hbm2java] at net.sf.hibernate.tool.hbm2java.Generator.write(Generator.java
:138)
[hbm2java] at net.sf.hibernate.tool.hbm2java.Generator.writeRecur(Generator
.java:115)
[hbm2java] at net.sf.hibernate.tool.hbm2java.Generator.generate(Generator.j
ava:104)
[hbm2java] at net.sf.hibernate.tool.hbm2java.CodeGenerator.main(CodeGenerat
or.java:125)
[hbm2java] at net.sf.hibernate.tool.hbm2java.Hbm2JavaTask.processFile(Hbm2J
avaTask.java:145)
[hbm2java] at net.sf.hibernate.tool.hbm2java.Hbm2JavaTask.execute(Hbm2JavaT
ask.java:95)
[hbm2java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.ja
va:275)
[hbm2java] at org.apache.tools.ant.Task.perform(Task.java:364)
...
skipped
...

Task definition looks like that

<target name="codegen">

<taskdef name="hbm2java"
classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
classpathref="project.class.path"/>

<hbm2java output="${src.dir}">
<fileset dir="${src.dir}">
<include name="**/*.hbm.xml"/>
</fileset>
</hbm2java>

</target>

project.class.path

<path id="project.class.path">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>

<fileset dir="${ms.jdbc.dir}">
<include name="*.jar"/>
</fileset>

</path>

I've copied all files from hibernate (and tools) lib to my project\lib directory.

Mapping file is simplistic:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.dti.tests.Location" table="LOCATION">
<id name="id" type="int" column="ID">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="description" type="string" not-null="true"/>
<property name="active" type="int" not-null="true"/>
</class>
</hibernate-mapping>

Still cannot generate class file!

Any explanation what i'm doing wrong? Notice, that i do not need to compile yet, just generate classes.
Yuri Khupchenko
Новичок
Posts: 37
Joined: 27 Apr 2004 15:25
Location: SPb

Post by Yuri Khupchenko »

NNemo wrote:That gona kill me...
....


Это уж вряд ли :lol: http://javaboutique.internet.com/resour ... ticle.html

Рекомендую взять примеры отсюда - http://www.oreilly.com/catalog/hibernate/, и на их основе делать что-то свое :)
NNemo
Уже с Приветом
Posts: 1935
Joined: 15 Sep 2003 17:49
Location: Ukraine, Mariupol -> USA everywhere :-)

Post by NNemo »

Bug in v. 2.1.6.

Some properties must be present in hibernate.cfg.xml file (even if set to default values), for correct code generation.

I have that book you suggested. Not really helpful yet, but it is better then nothing.

Return to “Вопросы и новости IT”