You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Radim Kolář <hs...@sendmail.cz> on 2010/11/19 21:08:13 UTC

@Basic (optional=false) not mapped to NOT NULL

I have following entity class:

@Entity
@Table(name="queue")

public class QueueEntry {

	@Id
	@GeneratedValue
	private int id;

	@Basic(optional=false)
	private String url;

	@Version
	private long version;
}

SQL statement used for table creation (i use H2 database) is:
CREATE TABLE queue (id INTEGER NOT NULL, URL VARCHAR(255), version INTEGER,
PRIMARY KEY (id));

note NOT NULL on URL column is missing.
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Basic-optional-false-not-mapped-to-NOT-NULL-tp5756491p5756491.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: @Basic (optional=false) not mapped to NOT NULL

Posted by Michael Dick <mi...@gmail.com>.
You're correct, @Column(nullable=false) is probably what the original poster
wants.

A good way to think of the two is in their scope. The optional attribute
pertains to the field or attribute on your entity.JPA providers can use this
at runtime to determine whether your object model is consistent. For example
OpenJPA uses it to determine whether it's safe to use an inner join instead
of an outer join.

@Column(nullable=true|false) is used to indicate the database schema - in
OpenJPA it's used primarily for schema generation.

The answer to this
question<http://stackoverflow.com/questions/2899073/basicoptional-false-vs-columnnullable-false-in-jpa>on
StackOverflow covers it well (originally answered by Gordon York).

Hope this helps,
-mike

On Fri, Nov 19, 2010 at 2:10 PM, ljnelson <lj...@gmail.com> wrote:

>
> For reasons that have never been clear to me, @Basic(optional = false) does
> not affect column nullability.  I am actually completely uncertain as to
> what it DOES do.
>
> You want:
>
> @Column(name = "url", nullable = false)
>
> Best,
> Laird
>
> On Fri, Nov 19, 2010 at 3:08 PM, Radim Kolář [via OpenJPA] <
> ml-node+5756491-910138721-155969@n2.nabble.com<ml...@n2.nabble.com>
> <ml...@n2.nabble.com>
> >
> > wrote:
>
> > I have following entity class:
> >
> > @Entity
> > @Table(name="queue")
> >
> > public class QueueEntry {
> >
> >         @Id
> >         @GeneratedValue
> >         private int id;
> >
> >         @Basic(optional=false)
> >         private String url;
> >
> >         @Version
> >         private long version;
> > }
> >
> > SQL statement used for table creation (i use H2 database) is:
> > CREATE TABLE queue (id INTEGER NOT NULL, URL VARCHAR(255), version
> INTEGER,
> > PRIMARY KEY (id));
> >
> > note NOT NULL on URL column is missing.
> >
> > ------------------------------
> >  View message @
> >
> http://openjpa.208410.n2.nabble.com/Basic-optional-false-not-mapped-to-NOT-NULL-tp5756491p5756491.html
> > To start a new topic under OpenJPA Users, email
> > ml-node+208411-242897574-155969@n2.nabble.com<ml...@n2.nabble.com>
> <ml...@n2.nabble.com>
> >
> > To unsubscribe from OpenJPA Users, click here<
> http://openjpa.208410.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=208411&code=bGpuZWxzb25AZ21haWwuY29tfDIwODQxMXw4OTk4MzY1OTI=
> >.
> >
> >
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Basic-optional-false-not-mapped-to-NOT-NULL-tp5756491p5756496.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: @Basic (optional=false) not mapped to NOT NULL

Posted by ljnelson <lj...@gmail.com>.
For reasons that have never been clear to me, @Basic(optional = false) does
not affect column nullability.  I am actually completely uncertain as to
what it DOES do.

You want:

@Column(name = "url", nullable = false)

Best,
Laird

On Fri, Nov 19, 2010 at 3:08 PM, Radim Kolář [via OpenJPA] <
ml-node+5756491-910138721-155969@n2.nabble.com<ml...@n2.nabble.com>
> wrote:

> I have following entity class:
>
> @Entity
> @Table(name="queue")
>
> public class QueueEntry {
>
>         @Id
>         @GeneratedValue
>         private int id;
>
>         @Basic(optional=false)
>         private String url;
>
>         @Version
>         private long version;
> }
>
> SQL statement used for table creation (i use H2 database) is:
> CREATE TABLE queue (id INTEGER NOT NULL, URL VARCHAR(255), version INTEGER,
> PRIMARY KEY (id));
>
> note NOT NULL on URL column is missing.
>
> ------------------------------
>  View message @
> http://openjpa.208410.n2.nabble.com/Basic-optional-false-not-mapped-to-NOT-NULL-tp5756491p5756491.html
> To start a new topic under OpenJPA Users, email
> ml-node+208411-242897574-155969@n2.nabble.com<ml...@n2.nabble.com>
> To unsubscribe from OpenJPA Users, click here<http://openjpa.208410.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=208411&code=bGpuZWxzb25AZ21haWwuY29tfDIwODQxMXw4OTk4MzY1OTI=>.
>
>

-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Basic-optional-false-not-mapped-to-NOT-NULL-tp5756491p5756496.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.