簡単な質問だと思います。私は両方の方法で例を見てきました。問題は、「フィールドに注釈を付けられないのはなぜですか?」ということです。例を挙げましょう....
@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;}
}
上記のコードは問題なく動作します (そこにタイプミスがないことを前提としています)。注釈がプロパティのゲッターに配置されると、すべてが完璧になります。
しかし、それは私には厄介なようです。私の考えでは、フィールドに注釈を配置する方がクリーンです。
@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;}
}
私は両方の方法の例を見てきました。ただし、この 2 番目の例を実行すると、次の結果が得られます...
java.lang.NullPointerException com.widget.util.hibernate.HibernateSessionFactory$ThreadLocalSession.initialValue (HibernateSessionFactory.java:25) で com.widget.util.hibernate.HibernateSessionFactory$ThreadLocalSession.initialValue(HibernateSessionFactory.java:1) で java.lang.ThreadLocal$ThreadLocalMap.getAfterMiss (不明なソース) で java.lang.ThreadLocal$ThreadLocalMap.get (不明なソース) で java.lang.ThreadLocal$ThreadLocalMap.access$000 で (不明なソース) java.lang.ThreadLocal.get (不明なソース) で com.widget.util.hibernate.HibernateSessionFactory.get (HibernateSessionFactory.java:33) で com.widget.db.dao.AbstractDao で。(AbstractDao.java:12) com.widget.db.dao.WidgetDao で。(WidgetDao.java:9) com.widget.db.dao.test.WidgetDaoTest.findById (WidgetDaoTest.java:17) で sun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブメソッド) sun.reflect.NativeMethodAccessorImpl.invoke (不明なソース) で sun.reflect.DelegatingMethodAccessorImpl.invoke (不明なソース) で java.lang.reflect.Method.invoke (不明なソース) で ...
HibernateSessionFactory
これが(25行目がマークされている)のスケルトンです....
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;
}
ここで何が起こっているのか誰にも分かりますか?