J'ai commencé à utiliser intensément groovy pour tout prototyper. Ceci est incroyable.
Mais j'ai fait face au problème avec coquille groovy.
Code suivant je cours avecclasses groovy et groovy visibilité et annotation analyse
groovy filename.groovy
et tout fonctionne comme prévu.
Mais à l'intérieur groovysh
commande
load filename.groovy
ne fonctionne pas: il ne peut pas trouver du livre de classe.
Le code:
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()
En fait, si vous écrivez Book
classe comme
@Entity
class Book {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long id
public String author
public String title
String toString() { "$title by $author" }
}
Groovy Shell ne comprendra pas les annotations lorsque vous exécutez
load filename.groovy
donc jouer avec JPQL Je dois déplacer Entity vers le fichier séparé, le groovyc et ensuite charger le shell groovy. Pas le pire des cas mais ce serait génial si je pouvais juste charger le prototype à l'intérieur de la coque.
Avez-vous des idées pour résoudre ce problème?
Je connais la solution de contournement. Je veux comprendre la différence entre la classe chargée de groovysh et la classe chargée groovy –