本文介绍了如何避免NullPointerException与OneToOne映射对父实体类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索并发现了类似的问题,但它们看起来并没有像









我有几个像这样映射的实体: / p>

  Person 
|
+ - User

我想添加一个新实体 PersonPartDeux 与OneToOne映射到 Person 。结果映射应该如下所示:

  Person + PersonPartDeux 
|
+ - User

当我这样做时,一个 NullPointerException 在尝试加载映射时抛出:

  java.lang.NullPointerException 
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)

如何指定映射,所以我可以避免这种异常?



这是我的代码:

  @Entity 
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Person实现Serializable
{
@Id
@GeneratedValue
public Long id ;

@版本
public int version = 0;

公共字符串名称;

@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
PersonPartDeux personPartDeux;

$ b $实体
public class PersonPartDeux实现可序列化的
{
@Id
@GeneratedValue(generator =person-primarykey)
@GenericGenerator(
name =person-primarykey,
strategy =foreign,
parameters = @Parameter(name =property,value =person)

public Long id = null;

@版本
public int version = 0;

@OneToOne(可选= false,mappedBy =person)
public Person person;

public String someText;

$ b $实体
@PrimaryKeyJoinColumn(name =person_Id)
public class User extends Person
{
public String username;
公共字符串密码;
}

至于为什么我很烦,我需要继承和 OneToOne 映射来解决我应用程序中不同的已知问题。

解决方案

Hibernate的源代码到你的项目中,所以你可以点击通过或'打开类型'(在Eclipse中的Ctrl-Shift-T)来查看OneToOneSecondPass源代码。



会给你一个明确的指示,说明需要指定什么。



在我的源代码(Hibernate 4.1.7)中,第135行是

  propertyHolder.addProperty(prop,inferredData.getDeclaringClass()); 

然而,您可能正在使用较早的版本。



查看映射,我怀疑@OneToOne定义 - mappedBy =person。

  @OneToOne(可选= false,mappedBy =person)
public Person person;

映射关联属性本身有什么意义? Hibernate已经知道这个属性是一个OneToOne - 你刚刚告诉过它。



指向属性的基本映射/ FK,本身......可能实际上并没有告诉Hibernate任何正确或有用的信息。



以下是HB dosc的一个例子,或许可以更好地表达您想要的:

  @实体
类MedicalHistory实现Serializable {
@Id Integer id;

@MapsId @OneToOne
@JoinColumn(name =patient_id)
病人;
}

@Entity
class Person {
@Id @GeneratedValue Integer id;
}

来源:
(关于JBoss网站的3.5个文档)。

干杯,希望这有助于。


I have searched and found similar issues, but they don't quite seem to be the same problem as

I have a few entities mapped like this:

Person
  |
  +--User

I want to add a new entity PersonPartDeux with a OneToOne mapping to Person. The resulting mapping should look something like this:

Person + PersonPartDeux
  |
  +--User

When I do so, a NullPointerException is thrown while trying to load the mapping:

java.lang.NullPointerException
    at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)

How do I specify the mapping so I can avoid this exception?

Here's my code:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Person implements Serializable
{
    @Id
    @GeneratedValue
    public Long id;

    @Version
    public int version = 0;

    public String name;

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public PersonPartDeux personPartDeux;
}

@Entity
public class PersonPartDeux implements Serializable
{
    @Id
    @GeneratedValue(generator = "person-primarykey")
    @GenericGenerator(
        name = "person-primarykey",
        strategy = "foreign",
        parameters = @Parameter(name = "property", value = "person")
    )
    public Long id = null;

    @Version
    public int version = 0;

    @OneToOne(optional=false, mappedBy="person")
    public Person person;

    public String someText;
}

@Entity
@PrimaryKeyJoinColumn(name = "person_Id")
public class User extends Person
{
    public String username;
    public String password;
}

As for why I'm bothering, I need both the inheritance and the OneToOne mapping to solve different known issues in my application.

解决方案

Attach the Hibernate source to your project, so you can click thru or 'Open Type' (Ctrl-Shift-T in Eclipse) to view the OneToOneSecondPass source.

Seeing the source, will give you a clear indication as to what needs to be specified.

In my source (Hibernate 4.1.7), line 135 is

propertyHolder.addProperty( prop, inferredData.getDeclaringClass() );

However you're probably using an earlier version.

Looking at the mappings, I'm suspicious of the @OneToOne definition -- mappedBy="person".

@OneToOne(optional=false, mappedBy="person")
public Person person;

What does it usefully mean, to map an association property by itself? Hibernate already knows the property is a OneToOne -- you just told it that.

Pointing the underpinning mapping/ FK of the property, at itself.. probably isn't actually telling Hibernate any correct or useful information.

Here's an example from the HB dosc, perhaps showing better how to do what you want:

@Entity
class MedicalHistory implements Serializable {
  @Id Integer id;

  @MapsId @OneToOne
  @JoinColumn(name = "patient_id")
  Person patient;
}

@Entity
class Person {
  @Id @GeneratedValue Integer id;
}

Source: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/(3.5 docs off JBoss site.)

Cheers, hope this helps.

这篇关于如何避免NullPointerException与OneToOne映射对父实体类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 18:06