Hi

currently i'm trying to use the persistence api. I stuck at inserting a one to many relationship. The problem is that the foreign key is always null.

I have two entity classes. User and Option.

User

Name Type DB Field Persistent Length Properties Association Cascade Mapped by
id Integer id yes id, generated
name String name yes 255
opts Listy<Option> yes ONE_TO_MANY persist, merge user_fk

Option

Name Type DB Field Persistent Length Properties Association Cascade Mapped by
id Integer id yes id, generated
option String opt_value yes 255
user_fk User user_fk yes not nullable MANY_TO_ONE persist, merge

The Option Objects are created from strings by a converter from a p:selectManyCheckbox. The field user_fk is not set manually in this converter because i don't have a reference to the User object in the converter.

Xpert.ivy creates an entity class for User and adds a "setOpts(List)" method for me.

public void setOpts(java.util.List< persistenceTest.Option > _opts) { opts = _opts; }

If there where a "addOpt(Option) method I could add the reference to User myself.

public void addOpt(persistenceTest.Option __opts) { _opts.setUser_fk(this); optsList.add(_opts); }

Am I thinking wrong or is it not possible to automate the creation of foreign keys?

Best Regards Harald

asked 26.03.2014 at 10:25

HaraldWeber's gravatar image

HaraldWeber
101101015
accept rate: 33%


I used a script step and called ivy.persistence.persistence.persist(in.user);

My database looks like this: CREATE TABLE [dbo].users ON [PRIMARY] GO

CREATE TABLE [dbo].opts ON [PRIMARY] GO

ALTER TABLE [dbo].[opts] WITH CHECK ADD CONSTRAINT [FK34282079933D49] FOREIGN KEY([user_fk_id]) REFERENCES [dbo].[users] ([id]) GO

I think the problem is how I create new Items for the opts list in the user class. In the view i have a selectManyCheckbox: <p:selectManyCheckbox id="select" value="#{data.user.opts}" > <f:selectItem itemLabel="Option 1" itemValue="Option 1" /> <f:selectItem itemLabel="Option 2" itemValue="Option 2" /> <f:converter converterId="conv.OptionConverter" /> </p:selectManyCheckbox>

The con.OptionConverter creates one option object from the given string. @Override public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException { Option option = new Option(); option.setOption(arg2); return option; }

There should also be something like option.setUser_fk(user); so the reference from User to Option is set. This is not possible in the converter because there is no reference to the User object. The only way to set the reference of User in Option is in the User Entity Class where the list is set (or built up). But the generated code only sets the list with empty references to User. So the persistence api can't know their connection. Maybe I'm thinking wrong and build the example wrong but without the references set the persistence api will never set the keys.

Best Regards Harald

link

answered 31.03.2014 at 09:19

HaraldWeber's gravatar image

HaraldWeber
101101015
accept rate: 33%

Now I set the reference of User ins Option myself and it is working now.

(02.04.2014 at 12:37) HaraldWeber HaraldWeber's gravatar image

Hi Harald,

did you try to persist the dataclass? ivy.persistence.xyz.persist(User);

as i know the keys are set within the database server.

in our case we used MSSQL and the keys are set by MSSQL table settings.

Best regards Roland

link

answered 27.03.2014 at 20:13

roland's gravatar image

roland
3465615
accept rate: 41%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×29

Asked: 26.03.2014 at 10:25

Seen: 1,425 times

Last updated: 02.04.2014 at 12:37