0

私はすべてのプロトタイプを作成するためにGroovyを集中的に使用し始めました。これは素晴らしいです。
しかし、私はグルーヴィーなシェルの問題に直面しました。
私が実行する次のコード

groovy filename.groovy

そして、すべてが期待どおりに機能します。
しかし、groovyshコマンド内

load filename.groovy

動作しません:クラスBookが見つかりません。

コード:

import org.hibernate.cfg.*
import org.hibernate.ejb.*
import javax.persistence.*

@Entity class Book {
    @Id @GeneratedValue(strategy = GenerationType.AUTO) public Long id
    public String author
    public String title
    String toString() { "$title by $author" }
}

hibernateProperties = [
    "hibernate.dialect": "org.hibernate.dialect.HSQLDialect",
    "hibernate.connection.driver_class": "org.hsqldb.jdbcDriver",
    "hibernate.connection.url": "jdbc:hsqldb:mem:demodb",
    "hibernate.connection.username": "sa",
    "hibernate.connection.password": "",
    "hibernate.connection.pool_size": "1",
    "hibernate.connection.autocommit": "true",
    "hibernate.cache.provider_class": "org.hibernate.cache.NoCacheProvider",
    "hibernate.hbm2ddl.auto": "create-drop",
    "hibernate.show_sql": "true",
    "hibernate.transaction.factory_class": "org.hibernate.transaction.JDBCTransactionFactory",
    "hibernate.current_session_context_class": "thread"
]

properties = new Properties()
hibernateProperties.each { k, v -> properties.setProperty(k, v) }
cfg = new Ejb3Configuration()

emf = cfg.addProperties(properties).addAnnotatedClass(Book.class).buildEntityManagerFactory()
em = emf.createEntityManager()
query = em.createQuery("SELECT b FROM Book b")

println query.getResultList()

実際、Bookクラスを次のように書くと

@Entity 
class Book {
    @Id @GeneratedValue(strategy = GenerationType.AUTO) 
    public Long id
    public String author
    public String title
    String toString() { "$title by $author" }
}

Groovy Shellは、実行時に注釈を理解しません

load filename.groovy

したがって、JPQLで遊ぶには、エンティティを別のファイルに移動し、それをgroovycしてから、groovyシェルをロードする必要があります。最悪のケースではありませんが、プロトタイプをシェル内にロードできれば素晴らしいと思います。

これを解決する方法について何かアイデアはありますか?

4

1 に答える 1

0

簡単に言えば、これを回避する方法はないと思います。Book クラスを頻繁に変更せずに再利用する場合は、いつでも一度コンパイルしてクラスパスに追加し、Groovy がシェルを使用するたびにそれを取得できるようにします。

于 2009-08-28T21:08:11.457 に答える