私はすべてのプロトタイプを作成するために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シェルをロードする必要があります。最悪のケースではありませんが、プロトタイプをシェル内にロードできれば素晴らしいと思います。
これを解決する方法について何かアイデアはありますか?