Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8070539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:25:14+00:00 2026-06-05T13:25:14+00:00

I’m trying to use cayenne to interact with a PostgreSQL database. So far everything

  • 0

I’m trying to use cayenne to interact with a PostgreSQL database. So far everything worked as expected but I’m now stuck with a problem I don’t really understand (I’m new to cayenne).

Say I have a table “books” with usual fields (book_id, title, editor, price), a “table” authors (author_id, author_name) and a link table “books_authors” (book_id, author_id).

All the classes associated with the process of adding or editing a book share one DataContext.

I have a struts2 (I use velocity templates) form that lets you add a book and, using ajax, multiple authors associated to it. I react to certain event to fire an ajax request that calls a struts2 action that verifies if the author exists and then adds a Books_Authors object (class generated by cayenne) to the struts2 session (the action implements SessionAware).

The form looks like this:

<form action="AddOrEditBook.do">
    <input type="hidden" name="book.book_id" value="$!book.book_id" />

    <input type="text" name="book.title" value="$!book.title" />

    <input type="text" name="book.editor" value="$!book.editor" />

    <input type="number" name="book.price" value="$!book.price" />

    <input type="text" name="author" /> <input type="button" name="addAuthor" value="Add" />

    <ul>
        <li id="author_2">Author 2 <input type="button" name="removeAuthor" value="Remove" /></li>
        <li id="author_16">Author 16 <input type="button" name="removeAuthor" value="Remove" /> </li>
    </ul>

    <input type="submit" />
</form>

Once I submit the form to AddOrEditBook.do that has a setter and getter for book, struts2 creates the book object and fills it with the values of the form. That’s nice.

This the thing that bothers me:

When I update a book, I didn’t find a way to use the object created by struts to update the db. What I want to achieve is to somehow instruct cayenne to use the object created by struts to update the corresponding entity in the db.

I’ve messed a lot with things like object_id, PersistenceState, etc.:

Expression exp_book = ExpressionFactory.matchExp(Book.BOOK_ID_PROPERTY, book.getBook_id());
Book bookFromDb = RetrieveHelper.retrieveScalarByExpression(ctxt, new Book(), exp_book);

book.setObjectContext(ctxt);
book.setObjectId(bookFromDb.getObjectId());
ctxt.getObjectStore().registerNode(book.getObjectId(), book);
book.setPersistenceState(PersistenceState.MODIFIED);

What is the correct strategy when using cayenne to update db entities? Is it only possible to do that by making things like bookFromDb.setTitle(book.getTitle()); ?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:25:16+00:00Added an answer on June 5, 2026 at 1:25 pm

    struts2 creates the book object and fills it with the values of the form. That’s nice.

    I am no Struts2 expert, but from what I am reading in the docs about form object lifecycle, such object is created by Struts2 by calling a default constructor. Unless there is a way to override this behavior, replacing it with some factory that would give you a Cayenne object from the right DataContext, what you are doing here is the right thing – you are separating the value object attached to the form (and the action class) from a persistent object.

    With other frameworks like Tapestry, etc. you can bind the form to a Cayenne persistent object directly. Maybe you can extend Struts2 in a similar fashion as well??

    All the classes associated with the process of adding or editing a book share one DataContext.

    An unrelated piece of advise. You must have a separate DataContext for each user at the minimum (e.g. place it in HttpSession). Or if you don’t keep uncommitted state between requests – maybe even one DataContext per request. In other words – do not share DataContexts unless they are read-only. Otherwise your app is not thread-safe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.