You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Henno Vermeulen <he...@huizemolenaar.nl> on 2011/03/11 16:52:21 UTC

entityManager.find returns null (even if a row with the exact id exists in the database)

Hello,

I have an entity with a column that is annotated with @ManyToOne(optional = false). The corresponding database table has a row that contains null for his column. When I try to find this entity by it's correct id using entityManager.findById, it returns null.

Is this expected behavior? I would have expected an error message.

Regards,
Henno Vermeulen
Huize Molenaar

RE: entityManager.find returns null (even if a row with the exact id exists in the database)

Posted by Henno Vermeulen <he...@huizemolenaar.nl>.
Allright thank you.

I actually did generate the database schema with OpenJPA and used manual SQL to insert values from a legacy database, but at the time I wrote the SQL I wasn't aware that there were actually rows with null values.

You mention that the "optional" flag is also used for generating schemas. This is what I actually would like to happen, but for me OpenJPA does not generate foreign key columns with nullable = false when I use @ManyToOne(optional = false). Is there a setting for this? I currently do use this setting

			<property name="openjpa.jdbc.MappingDefaults"
				value="ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict" />

to generate the foreign key constraint, but the foreign key columns are still nullable. One way I know that works is to also use the annotation @JoinColumn(nullable=false), but this seems to me as unnecessary code duplication.

Henno

-----Oorspronkelijk bericht-----
Van: Kevin Sutter [mailto:kwsutter@gmail.com] 
Verzonden: vrijdag 11 maart 2011 18:28
Aan: users@openjpa.apache.org
Onderwerp: Re: entityManager.find returns null (even if a row with the exact id exists in the database)

Hi Henno,
The JPA spec is not prescriptive on the behavior in this case.  You
annotated the relationship to be not optional, but since you already have
rows in the database table that contain null values, we don't flag this as
an error.  Other providers may have different behavior.  In many cases, this
"optional" flag is used when generating database schemas in order to
generate the proper constraints.

Kevin

On Fri, Mar 11, 2011 at 9:52 AM, Henno Vermeulen <he...@huizemolenaar.nl>wrote:

> Hello,
>
> I have an entity with a column that is annotated with @ManyToOne(optional =
> false). The corresponding database table has a row that contains null for
> his column. When I try to find this entity by it's correct id using
> entityManager.findById, it returns null.
>
> Is this expected behavior? I would have expected an error message.
>
> Regards,
> Henno Vermeulen
> Huize Molenaar
>

Re: entityManager.find returns null (even if a row with the exact id exists in the database)

Posted by Kevin Sutter <kw...@gmail.com>.
Hi Henno,
The JPA spec is not prescriptive on the behavior in this case.  You
annotated the relationship to be not optional, but since you already have
rows in the database table that contain null values, we don't flag this as
an error.  Other providers may have different behavior.  In many cases, this
"optional" flag is used when generating database schemas in order to
generate the proper constraints.

Kevin

On Fri, Mar 11, 2011 at 9:52 AM, Henno Vermeulen <he...@huizemolenaar.nl>wrote:

> Hello,
>
> I have an entity with a column that is annotated with @ManyToOne(optional =
> false). The corresponding database table has a row that contains null for
> his column. When I try to find this entity by it's correct id using
> entityManager.findById, it returns null.
>
> Is this expected behavior? I would have expected an error message.
>
> Regards,
> Henno Vermeulen
> Huize Molenaar
>