0
私はgroovyを使ってすべてを試作し始めました。これは素晴らしいです。
しかし、私はgroovyシェルで問題に直面しました。
次のコードは、私がgroovyshとgroovyクラス可視性とアノテーションの解析
groovy filename.groovy
で実行し、すべてが期待どおりに動作します。
しかしgroovysh
コマンド内
load filename.groovy
は動作しません:それはクラスブックを見つけることができません。
コード:実際には
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()
あなたは
あなたがそうと遊ぶのがload filename.groovy
を実行したときにGroovyのシェルは注釈を理解することはありません
@Entity
class Book {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long id
public String author
public String title
String toString() { "$title by $author" }
}
としてBook
クラスを記述する場合JPQL Entityを別のファイルに移動し、groovycを実行してGroovyシェルをロードする必要があります。最悪のケースではなく、シェルの内側にプロトタイプをロードするだけで大丈夫です。
これを解決する方法はありますか?
私は回避策を知っています。私はGroovyshクラスとGroovyクラスをロードするクラスの違いを理解したい –