Статьи

NuoDB и Hibernate

Для пользователей Java NuoDB предоставляет диалект Hibernate, который поддерживает версии 3.6.6 и 4.1.x Hibernate.

Пример приложения, использующего Hibernate и диалект NuoDB Hibernate, можно найти в каталоге примеров установки NuoDB:

  • % NUODB_HOME% \ samples \ hibernate (Windows)
  • $ NUODB_HOME / samples / hibernate (UNIX)

Образец приложения построен с использованием Maven, поэтому в ваш репозиторий Maven необходимо установить и диалект NuoDB, и файлы JAR JDBC, прежде чем пытаться создать и запустить образец.

Jar-файлы JDBC Hibernate находятся в:

  • % NUODB_HOME% \ jar \ nuodb-hibernate-1.0.jar (Windows)
  • $ NUODB_HOME / jar / nuodb-hibernate-1.0.jar (UNIX)
  • % NUODB_HOME% \ jar \ nuodbjdbc.jar (Windows)
  • $ NUODB_HOME / jar / nuodbjdbc.jar (UNIX)

Чтобы установить файлы jar в локальный репозиторий (для Windows просто измените путь):

/opt/nuodb/samples/hibernate $ mvn install:install-file -DgroupId=com.nuodb -DartifactId=nuodb-jdbc -Dversion=1.0 -Dpackaging=jar -Dfile=/opt/nuodb/jar/nuodbjdbc.jar
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Hibernate Sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install-file (default-cli) @ nuodb-hibernate-sample ---
[INFO] Installing /opt/nuodb/jar/nuodbjdbc.jar to /Users/johnblais/.m2/repository/com/nuodb/nuodb-jdbc/1.0/nuodb-jdbc-1.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.587s
[INFO] Finished at: Thu Apr 11 11:58:59 EDT 2013
[INFO] Final Memory: 3M/81M
[INFO] ------------------------------------------------------------------------

/opt/nuodb/samples/hibernate $ mvn install:install-file -DgroupId=com.nuodb -DartifactId=nuodb-hibernate -Dversion=1.0 -Dpackaging=jar -Dfile=/opt/nuodb/jar/nuodb-hibernate-1.0.jar [INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Hibernate Sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install-file (default-cli) @ nuodb-hibernate-sample ---
[INFO] Installing /opt/nuodb/jar/nuodb-hibernate-1.0.jar to /Users/johnblais/.m2/repository/com/nuodb/nuodb-hibernate/1.0/nuodb-hibernate-1.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.501s
[INFO] Finished at: Thu Apr 11 11:59:29 EDT 2013
[INFO] Final Memory: 3M/81M
[INFO] ------------------------------------------------------------------------

Чтобы создать приложение:

/opt/nuodb/samples/hibernate $ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Hibernate Sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ nuodb-hibernate-sample ---
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ nuodb-hibernate-sample ---
[INFO] Compiling 4 source files to /opt/nuodb/samples/hibernate/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.302s
[INFO] Finished at: Thu Apr 11 11:59:41 EDT 2013
[INFO] Final Memory: 9M/81M
[INFO] ------------------------------------------------------------------------

Чтобы выполнить заявку:

/opt/nuodb/samples/hibernate $ mvn exec:java
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Hibernate Sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.1:java (default-cli) @ nuodb-hibernate-sample >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.1:java (default-cli) @ nuodb-hibernate-sample <<<
[INFO]
[INFO] --- exec-maven-plugin:1.1:java (default-cli) @ nuodb-hibernate-sample ---
Found 2 user records:
User (1/0), Username: fred, Name: Fred Flintstone, admin
home: Street: '301 Cobblestone Way', Zipcode: '00001', City: 'Bedrock'
bill: Street: '301 Cobblestone Way', Zipcode: '00001', City: 'Bedrock'
ship: Street: '1 Slate Drive', Zipcode: '00002', City: 'Granitetown'
User (2/0), Username: barney, Name: Barney Rubble, member
home: Street: '303 Cobblestone Way', Zipcode: '00001', City: 'Bedrock'
bill: Street: '303 Cobblestone Way', Zipcode: '00001', City: 'Bedrock'
ship: Street: '1 Slate Drive', Zipcode: '00002', City: 'Granitetown'
Found 2 user records:
Found 2 user records:
User (1/1), Username: fred, Name: FRED FLINTSTONE, member
home: Street: '301 Cobblestone Way', Zipcode: '00001', City: 'Bedrock'
bill: Street: '301 COBBLESTONE WAY', Zipcode: '00001', City: 'BEDROCK'
ship: Street: '1 Slate Drive', Zipcode: '00002', City: 'Granitetown'
User (2/1), Username: barney, Name: BARNEY RUBBLE, member
home: Street: '303 Cobblestone Way', Zipcode: '00001', City: 'Bedrock'
bill: Street: '303 COBBLESTONE WAY', Zipcode: '00001', City: 'BEDROCK'
ship: Street: '1 Slate Drive', Zipcode: '00002', City: 'Granitetown'
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.692s
[INFO] Finished at: Thu Apr 11 11:59:53 EDT 2013
[INFO] Final Memory: 3M/81M
[INFO] ------------------------------------------------------------------------
/opt/nuodb/samples/hibernate $

Чтобы проверить результаты, мы можем использовать nuosql для проверки данных:

/opt/nuodb/samples/hibernate $ /opt/nuodb/bin/nuosql --user cloud --password user test@localhost
SQL> show schemas
 
    Schemas containing tables
 
        SAMPLE
        SYSTEM
SQL> use SAMPLE;
SQL> show tables;
 
    Tables in schema SAMPLE
 
        ADDRESS
        BILLING_ADDRESS
        USERS
SQL> select * from users;
 
 USER_ID  OBJ_VERSION  FIRSTNAME   LASTNAME  USERNAME  PASSWORD        EMAIL        RANK  IS_ADMIN          CREATED             HOME_STREET     HOME_ZIPCODE  HOME_CITY  
 -------- ------------ ---------- ---------- --------- --------- ------------------ ----- --------- ----------------------- ------------------- ------------- ---------- 
 
    1          1         FRED     FLINTSTONE  fred               [email protected]    0       0     2013-04-11 11:59:53.548 301 Cobblestone Way     00001      Bedrock   
    2          1         BARNEY   RUBBLE      barney             [email protected]   0       0     2013-04-11 11:59:53.583 303 Cobblestone Way     00001      Bedrock   
 
SQL>

Заметки

Для тех из вас, кто знаком с Hibernate, приложение использует файлы конфигурации Hibernate. Это можно было бы сделать так же легко, используя аннотации Java. Для получения дополнительной информации о Hibernate см. Http://www.hibernate.org/docs, начните с «Руководства по началу работы» для своей версии.

Чтобы узнать больше о Maven, см. Http://maven.apache.org/guides/

Неподдерживаемые функции Hibernate

NuoDB в настоящее время не поддерживает прокручиваемые наборы результатов.