mariosangiorgio
mariosangiorgio

Reputation: 5543

Hibernate: can I define a OneToOne relationship that takes place only if the target entity does exists?

I am having troubles loading an entity that contains an optional one-to-one relationship with another entity.

When I try to load an entity with the corresponding mapping everything seems to be ok, but sometimes (I guess it is when I try to load a CitationInformation without the corresponding VenueTopic) I get the following exception:

org.hibernate.PropertyAccessException: could not set a field value by reflection setter of

These are my classes:

@Entity
public class CitationInformation {
    @Id
    @GeneratedValue
    private long identifier;

    private String doi;

    @OneToOne(optional = true, targetEntity = VenueTopic.class)
    @JoinColumn(name = "venue")
    @NotFound(action=NotFoundAction.IGNORE)
    private String venue;
    ...
}

@Entity
public class VenueTopic {
    @Id
    private String venueName;
    private String topic;
    ...
}

Unfortunately I need to deal with this dirty situation because I had to integrate my hibernate entities with data from another table that was not generated through hibernate.

I think I messed something up in the @OneToOne relationship. I am not sure I defined correctly the fact that it is optional and how I want missing values to me handled.

The behavior I want is that CitationInformation should always contain a venue, but the relationship should exist only if there is a VenueTopic entity with the same venueName.

Upvotes: 0

Views: 131

Answers (1)

suenda
suenda

Reputation: 693

I think changing private String venue in your class CitationInformation from type String to type VenuTopic might solve the problem. I do not understand why you are using type string when you are already mapping it to another entity.

Upvotes: 1

Related Questions