Skip to Main Content

Integration

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

@ManyToOne(optional=false) and @JoinColumn(nullable=false) does not work

736353Nov 22 2009 — edited Nov 23 2009
Hi

This probably is a JPA amateur question please excuse me if I am being really naive here.

I have got two entity objects PixUser and Album [with 1-->M relationship]

@Entity
public class PixUser implements Serializable {
......
@OneToMany(targetEntity=Album.class,mappedBy="pixUser", cascade=CascadeType.ALL)
Collection<Album> albums = new ArrayList<Album>();
.....
}

@Entity
public class Album implements Serializable {
....
@ManyToOne(fetch=FetchType.EAGER,optional=false)
@JoinColumn(name="PIXUSER_USERNAME",nullable=false,updatable=true)
private PixUser pixUser;
.....
}

If I try and persist an Album object without setting the PixUser in the Album object, as per my understanding it shall give me an error that "There cannot be null for ALBUM.PIXUSER_USERNAME column" or something similar. But instead it creates a row in database successfully.

Code fragment:
Album album = new Album("My Album without a PixUser");
albumRepository.persistAlbum(album);

Cut log:
[TopLink Fine]: 2009.11.22 08:08:31.156--ClientSession(4980331)--Connection(19682353)--Thread(Thread[main,5,main])--INSERT INTO ATUL.ALBUM (ALBUM_ID, CREATION_DATE, MODIFICATION_DATE, ALBUM_NAME, CREATION_USER, MODIFICATION_USER, ALBUM_DESCRIPTION, PIXUSER_USERNAME) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
bind => [452, 2009-11-22 20:08:30.875, null, My Album without a PixUser, null, null, null, null]

Is this a bug or I did not understood the usage of optional &/or nullable correctly ?

Thanks
Atul
This post has been answered by Cdelahun-Oracle on Nov 23 2009
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 21 2009
Added on Nov 22 2009
3 comments
6,936 views