Hibernate的注解安置问题 [英] Hibernate Annotation Placement Question

查看:154
本文介绍了Hibernate的注解安置问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么,我认为是一个简单的问题。我见过的例子是双向的。现在的问题是 - 为什么我不能把我的注释在球场上?。我给大家举一个例子....

I've got what I think is a simple question. I've seen examples both ways. The question is - "why can't I place my annotations on the field?". Let me give you an example....

@Entity
@Table(name="widget")
public class Widget {
 private Integer id;

 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 public Integer getId() { return this.id; }
 public Integer setId(Integer Id) { this.id = id;}
}

以上code工作正常(假设有没有一个错字在那里)。当注释被放置在财产一切的getter是完美的。

The above code works fine (assuming there's not a typo in there). When the annotation is placed on the getter of the property everything is perfect.

不过,似乎尴尬的我。在我的脑海里是清洁的地方注释在球场上,像这样 -

However, that seems awkward to me. In my mind it's cleaner to place the annotation on the field, like so --

@Entity
@Table(name="widget")
public class Widget {
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 private Integer id;

 public Integer getId() { return this.id; }
 public Integer setId(Integer Id) { this.id = id;}
}

我见过的这两种方法的例子。然而,当我运行第二个例子我得到以下...

I've seen examples of both ways. However, when I run this second example I get the following...


java.lang.NullPointerException
    at com.widget.util.hibernate.HibernateSessionFactory$ThreadLocalSession.initialValue(HibernateSessionFactory.java:25)
    at com.widget.util.hibernate.HibernateSessionFactory$ThreadLocalSession.initialValue(HibernateSessionFactory.java:1)
    at java.lang.ThreadLocal$ThreadLocalMap.getAfterMiss(Unknown Source)
    at java.lang.ThreadLocal$ThreadLocalMap.get(Unknown Source)
    at java.lang.ThreadLocal$ThreadLocalMap.access$000(Unknown Source)
    at java.lang.ThreadLocal.get(Unknown Source)
    at com.widget.util.hibernate.HibernateSessionFactory.get(HibernateSessionFactory.java:33)
    at com.widget.db.dao.AbstractDao.(AbstractDao.java:12)
    at com.widget.db.dao.WidgetDao.(WidgetDao.java:9)
    at com.widget.db.dao.test.WidgetDaoTest.findById(WidgetDaoTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    ...

下面是骨架 HibernateSessionFactory (第25行标)......

Here's the skeleton of HibernateSessionFactory (line 25 is marked) ....

protected Session initialValue() {
	SessionFactory sessionFactory = null;
	try {
		Configuration cfg = new AnnotationConfiguration().configure();
		String url = System.getProperty("jdbc.url");
		if (url != null) {
			cfg.setProperty("hibernate.connection.url", url);
		}
		sessionFactory = cfg.buildSessionFactory();
	}
	catch (Exception e) {
	}

	Session session = sessionFactory.openSession();  // LINE 25
	return session;
}

任何人有一个想法,这是怎么回事呢?

Anyone have an idea what's going on here?

推荐答案

从性能和设计的角度来看,使用的干将注释比成员变量更好的主意,因为吸气制定者正在使用反射如果放在外地调用,比的方法。此外,如果你打算使用验证和Hibernate的其他功能,你将拥有所有的注解在一个地方,而不是分散他们所有的地方。

From a performance and design perspective, using annotations on getters is a better idea than member variables, because the getter setters are called using reflection if placed on the field, than a method. Also if you plan to use validation and other features of hibernate, you'll have all the annotations at one place, rather than scattering them all over the place.

我的建议去与方法不是成员变量。

My recommendation go with methods not member variables.

从文档

根据是否注释字段或方法,Hibernate使用的访问类型将字段或属性。在EJB3规范要求,如果你使用属性访问,如果您使用字段访问该声明将要访问的元素类型的注释,即getter方法​​,该领域。在这两个领域和方法混合EJB3注解应尽量避免。 Hibernate会猜测从@Id或​​@EmbeddedId的位置访问类型。

Depending on whether you annotate fields or methods, the access type used by Hibernate will be field or property. The EJB3 spec requires that you declare annotations on the element type that will be accessed, i.e. the getter method if you use property access, the field if you use field access. Mixing EJB3 annotations in both fields and methods should be avoided. Hibernate will guess the access type from the position of @Id or @EmbeddedId.

这篇关于Hibernate的注解安置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆