我正在使用 Vaadin 7 + Grails 2.3,有一些问题
我的域类
class Base {
private static final Date NULL_DATE = new Date(0)
Date createdAt = NULL_DATE;
Date updatedAt = NULL_DATE;
def beforeInsert(){
createdAt = new Date();
updatedAt = new Date();
}
def beforeUpdate(){
updatedAt = new Date();
}
static mapping = {
autoTimestamp true
}
}
abstract class Person extends Base{
String name;
String name2;
String phone1;
String phone2;
static constraints = {
name2 nullable:true
phone1 nullable:true
phone2 nullable:true
}
}
class Customer extends Person {
double credit;
}
问题
问题 1
在我的 Vaadin 类 UI 中,如果我尝试这个
class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
Customer customer = new Customer()
customer.name="RODRIGO"
customer.save()
}
}
显示此错误
servlet [VaadinServlet 0] 的 Servlet.service() 在路径 [/AgileWeb] 的上下文中抛出异常 [com.vaadin.server.ServiceException: groovy.lang.MissingPropertyException: 没有这样的属性:类的名称:agileweb.Customer
可能的解决方案:全部] 有根本原因
消息:没有这样的属性:类名称:agileweb.Customer
可能的解决方案:全部
没有“名称”属性? Customer 类扩展了具有此属性的 Person。
问题 2
如果我试试这个
Customer customer = new Customer()
Customer.setName("RODRIGO")
Customer.save()
显示错误:消息:没有 Hibernate Session 绑定(bind)到线程,并且配置不允许在此处创建非事务性 session
我已经搜索过这个错误,但我不明白要修复它,也许我是 Grails 的新手
问题 3 - 实际上是一个问题
我知道我可以使用 BeanItemContainer,只是来自 List 对象,这可能没有问题,但是 BeanItemContainer 不是延迟加载,我想使用 HbnContainer 插件(https://vaadin.com/directory#addon/hbncontainer)因为它只需要一个休眠 session ,所以如何我可以得到“Hibernante session”,有没有例子以及如何做?
问题 4 - 实际上是一个问题(再次)
按照本教程 https://vaadin.com/wiki/-/wiki/Main/Vaadin%20on%20Grails%20-%20Database%20access
它可以将对象保存在数据库中,但是问题
- 真的有必要为每个域类创建一个服务吗?我已经读过建议将域逻辑放在服务中,我同意这一点,但是不需要任何逻辑的简单域呢?
我知道有很多问题,但我认为这些问题与其他问题相同,我真的很想使用 Vaadin + Grails 来享受两者的优势,但目前对我来说并不容易!
最佳答案
在我开始回答你的问题之前,让我质疑你的领域模型。基类一般都很好,但我想谈谈 Person 和 Customer。您可能有充分的理由选择继承,但请考虑组合。也许你可以拥有一个包含对枚举的引用的 Person,它说明了这个人的类型。你可以从这里开始:http://en.wikipedia.org/wiki/Composition_over_inheritance
类 GenericService { def save(def domain) { domain.save(failOnError:true) }}
将 org.codehaus.groovy.grails.commons.ApplicationHolder 导入为 AH
def ctx = AH.application.mainContext
def sessionFactory = ctx.sessionFactory
或者
ApplicationContext applicationContext = grailsApplication.mainContext
ConfigurableLocalSessionFactoryBean factory = applicationContext.getBean('&sessionFactory')
关于grails - 使用 Vaadin 7 + Grails 2.3 的基本问题(持久化、领域类设计、获取 Hibernate Session),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20736237/