Thank you very much for your kind and very quick answer! I will try this
right now. You are right on the navegability on that association, and also
on the activity_id column in the WeekActivity table. Let's see if i get this
to work!
Thanks again :)
Best regards.
Michael Horwitz wrote:
>
> I think the problem is in the association you have attached to the
> getActivity() method in WeekActivity. I am assuming the association
> between
> WeekActivity and Activity is uni-directional and can only be navigated
> from
> WeekActivity to Activity? I am also assuming that the table for week
> activity contains a column called activity_id? In which case your
> association mapping should read:
>
> /**
> * @hibernate.many-to-one cascade="save-update"
> column="activity_id"
> unique="true"
> * @return the activity
> */
> public Activity getActivity() {
> return activity;
> }
>
> You should not need to save the Activity separately in your controller,
> nor
> should you need to explicitly set the id.
>
> Also a note on your controller code: you seem to be doing a lot of manual
> binding in your controllers. Spring can do a lot of this work for you -
> just
> take a look at the form tags:
> http://static.springframework.org/spring/docs/2.0.x/reference/spring-form.tld.html
> and
> this discussion on the user mailing list on binding custom property
> editors:
> http://tinyurl.com/2f68de (just look down the list for discussion on using
> Spring form tags).
>
> Mike.
>
>
> On 7/16/07, Sielm <[EMAIL PROTECTED]> wrote:
>>
>>
>> By the way, i forgot to mention that the Activity row is actually saved
>> into
>> the database correctly, but i get the error when trying to persist the
>> WeekActivity entity, so the WeekActivity table is always empty...
>>
>> Best regards.
>>
>>
>> Sielm wrote:
>> >
>> > Hi again. Here are the details i've worked on so far:
>> >
>> > /**
>> > * @hibernate.class table="activity"
>> > */
>> > public class Activity extends PersistentObject {
>> >
>> > private String name;
>> > private Float mondayHours;
>> > private Float tuesdayHours;
>> > private Float wednesdayHours;
>> > private Float thursdayHours;
>> > private Float fridayHours;
>> > private Float saturdayHours;
>> > private Float sundayHours;
>> >
>> > /**
>> > * @hibernate.id column="id" generator-class="native"
>> > */
>> > public String getId() {
>> > return super.getId();
>> > }
>> >
>> > /**
>> > * @hibernate.property column="name"
>> > * @return the name
>> > */
>> > public String getName() {
>> > return name;
>> > }
>> >
>> > /**
>> > * @hibernate.property column="monday_hours"
>> > * @return mondayHours
>> > */
>> > public Float getMondayHours() {
>> > return mondayHours;
>> > }
>> >
>> > /**
>> > * @spring.validator type="required"
>> > * @spring.validator type="maxlength"
>> > * @spring.validator-args arg1value="${var:maxlength}"
>> > * @spring.validator-var name="maxlength" value="100"
>> > * @param name the name to set
>> > */
>> > public void setName(String name) {
>> > this.name = name;
>> > }
>> >
>> > /**
>> > * @spring.validator type="float"
>> > * @spring.validator type="floatRange" msgkey="
>> errors.mondayhours.range"
>> > * @spring.validator-args arg1value="${var:min}"
>> > * @spring.validator-args arg2value="${var:max}"
>> > * @spring.validator-var name="min" value="0.5"
>> > * @spring.validator-var name="max" value="8.0"
>> > * @param mondayHours the mondayHours to set
>> > */
>> > public void setMondayHours(Float mondayHours) {
>> > this.mondayHours = mondayHours;
>> > }
>> >
>> >
>> ------------------------------------------------------------------------------------------------
>> >
>> > I just included 2 of the setter/getter methods because they are all
>> > similar. On the other side, i have the other class, the big one,
>> > WeekActivity:
>> >
>> > /**
>> > * @hibernate.class table="week_activity"
>> > */
>> > public class WeekActivity extends PersistentObject{
>> >
>> > private User user;
>> > private Week week;
>> > private Activity activity = new Activity();
>> > private Boolean isAuthorized = Boolean.FALSE;
>> >
>> > /**
>> > * @hibernate.id column="id" generator-class="native"
>> > */
>> > public String getId() {
>> > return super.getId();
>> > }
>> >
>> > /**
>> > * @hibernate.one-to-one cascade="save-update"
>> > * @return the activity
>> > */
>> > public Activity getActivity() {
>> > return activity;
>> > }
>> >
>> > /**
>> > * @hibernate.property column="is_authorized"
>> > * @return the isAuthorized
>> > */
>> > public Boolean getIsAuthorized() {
>> > return isAuthorized;
>> > }
>> >
>> > /**
>> > * @hibernate.many-to-one column="user_id" cascade="save-update"
>> > * @return the user
>> > */
>> > public User getUser() {
>> > return user;
>> > }
>> >
>> > /**
>> > * @hibernate.many-to-one column="week_id" cascade="save-update"
>> > * @return the week
>> > */
>> > public Week getWeek() {
>> > return week;
>> > }
>> >
>> > /**
>> > * @spring.validator type="required"
>> > * @param activity the activity to set
>> > */
>> > public void setActivity(Activity activity) {
>> > this.activity = activity;
>> > }
>> >
>> > /**
>> > * @spring.validator type="required"
>> > * @param isAuthorized the isAuthorized to set
>> > */
>> > public void setIsAuthorized(Boolean isAuthorized) {
>> > this.isAuthorized = isAuthorized;
>> > }
>> >
>> > /**
>> > * @spring.validator type="required"
>> > * @param user the user to set
>> > */
>> > public void setUser(User user) {
>> > this.user = user;
>> > }
>> >
>> > /**
>> > * @spring.validator type="required"
>> > * @param week the week to set
>> > */
>> > public void setWeek(Week week) {
>> > this.week = week;
>> > }
>> > }
>> >
>> >
>> ------------------------------------------------------------------------------------------------
>> >
>> > Like i said previously, when i try to save the whole WeekActivity from
>> the
>> > jsp, i fill the user.id and week.id fields on the onSubmit method in
>> the
>> > WeekActivityFormController. But, i am not able to also persist the
>> > activity (so it gets a newly generated ID each time) before that. And i
>> > need to do this because i need a new activity.id each time i fill the
>> > WeekActivity form (the activity.id will be unique in the WeekActivity
>> > table). This is what i have so far in the controller:
>> >
>> > protected void onBind(HttpServletRequest request, Object command,
>> > BindException errors) throws Exception {
>> >
>> > super.onBind(request, command);
>> >
>> > WeekActivity weekActivity = (weekActivity) command;
>> >
>> > // Here we get parameters to fill in User and Week
>> > objects...
>> > String userId = request.getParameter("user.id");
>> > String weekId = request.getParameter("week.id");
>> >
>> > // We obtain the User and Week from the request
>> parameters
>> > // because User and Week are already in the database...
>> > if (userId != null) {
>> >
>> > if (weekActivity.getUser() == null) {
>> >
>> > User user = (User)
>> manager.get(User.class,
>> userId);
>> > weekActivity.setUser(user);
>> > }
>> > }
>> >
>> > if (weekId != null) {
>> >
>> > if (weekActivity.getWeek() == null) {
>> >
>> > Week week = (Week)
>> manager.get(Week.class,
>> weekId);
>> > weekActivity.setWeek(week);
>> > }
>> > }
>> >
>> > // Now i try to set the corresponding Activity object
>> too...
>> > Activity theActivity = weekActivity.getActivity();
>> >
>> > // Setting Activity properties from parameters too
>> (except
>> > the ID!)
>> > String activityName = request.getParameter("activity.name
>> ");
>> > if (activityName != null && (!StringUtils.isEmpty(
>> activityName.trim())))
>> > {
>> > theActivity.setName(activityName);
>> > }
>> >
>> > String activityMon = request.getParameter("
>> activity.mondayHours");
>> > if (activityMon != null && (!StringUtils.isEmpty(
>> activityMon.trim()))) {
>> > theActivity.setMondayHours(new Float(
>> Float.parseFloat(activityMon)));
>> > }
>> >
>> > String activityTue = request.getParameter("
>> activity.tuesdayHours");
>> > if (activityTue != null && (!StringUtils.isEmpty(
>> activityTue.trim()))) {
>> > theActivity.setTuesdayHours(new Float(
>> Float.parseFloat(activityTue)));
>> > }
>> >
>> > String activityWed = request.getParameter("
>> activity.wednesdayHours");
>> > if (activityWed != null && (!StringUtils.isEmpty(
>> activityWed.trim()))) {
>> > theActivity.setWednesdayHours(new
>> > Float(Float.parseFloat(activityWed)));
>> > }
>> >
>> > String activityThu = request.getParameter("
>> activity.thursdayHours");
>> > if (activityThu != null && (!StringUtils.isEmpty(
>> activityThu.trim()))) {
>> > theActivity.setThursdayHours(new Float(
>> Float.parseFloat(activityThu)));
>> > }
>> >
>> > String activityFri = request.getParameter("
>> activity.fridayHours");
>> > if (activityFri != null && (!StringUtils.isEmpty(
>> activityFri.trim()))) {
>> > theActivity.setFridayHours(new Float(
>> Float.parseFloat(activityFri)));
>> > }
>> >
>> > String activitySat = request.getParameter("
>> activity.saturdayHours");
>> > if (activitySat != null && (!StringUtils.isEmpty(
>> activitySat.trim()))) {
>> > theActivity.setSaturdayHours(new Float(
>> Float.parseFloat(activitySat)));
>> > }
>> >
>> > String activitySun = request.getParameter("
>> activity.sundayHours");
>> > if (activitySun != null && (!StringUtils.isEmpty(
>> activitySun.trim()))) {
>> > theActivity.setSundayHours(new Float(
>> Float.parseFloat(activitySun)));
>> > }
>> >
>> > // Saving the activity now makes MySQL generate a new
>> id...
>> > // Not sure if i should do the following lines but
>> these
>> > are the last i tried...
>> >
>> > manager.save(theActivity);
>> >
>> > String theActivityId = theActivity.getId();
>> > theActivity.setId(theActivityId);
>> >
>> > weekActivity.setActivity(theActivity);
>> > }
>> >
>> >
>> ------------------------------------------------------------------------------------------------
>> >
>> > As you can see above, i pass some activity properties in the request
>> (all
>> > of them actually, except the ID!). So i fill the Activity object too,
>> > persist it so i get the ID (should this be necessary?) and the get the
>> ID
>> > again and set this Activity into the big WeekActivity object. The bad
>> part
>> > is when i run this... i get the following:
>> >
>> > Hibernate operation: could not insert: [WeekActivity]; uncategorized
>> > SQLException for SQL [insert into week_activity (is_authorized,
>> user_id,
>> > week_id) values (?, ?, ?)]; SQL state [HY000]; error code [1364]; Field
>> > 'activity' doesn't have a default value; nested exception is
>> > java.sql.SQLException: Field 'activity' doesn't have a default value
>> >
>> > Obviously it can't have a default value, because the ID should be
>> > generated each time and before saving the "big" WeekActivity.
>> >
>> > I have already wasted a week trying to solve this...I'm pretty new to
>> J2EE
>> > and much more to Hibernate and Spring so please bear with me and my
>> ugly
>> > code ;) but i'm desperate! Ther must be something i am doing
>> > wrong...maybe not just one thing but more...
>> >
>> > Thanks a lot for reading and lending a hand, you are the best! :)
>> >
>> > Best regards.
>> >
>> >
>> > Michael Horwitz wrote:
>> >>
>> >> O.K. It should all work as long as you have the relationship between
>> >> WeekActivity and Activity marked as cascade=save. If you are having
>> >> trouble
>> >> with the mapping/annotations just post up a sample of what you have so
>> >> far,
>> >> and we can help.
>> >>
>> >> Mike.
>> >>
>> >> On 7/14/07, Sielm <[EMAIL PROTECTED]> wrote:
>> >>>
>> >>>
>> >>> Sorry, i forgot about these details. I'm using AppFuse 1.9.4 with
>> Spring
>> >>> for
>> >>> the front end too, and Hibernate for persistence. The actual database
>> is
>> >>> MySQL 5.x (not sure about the exact version).
>> >>>
>> >>> Thanks a lot. If i forget anything else please tell me and i will
>> reply
>> >>> as
>> >>> soon as possible.
>> >>>
>> >>> Best regards.
>> >>>
>> >>>
>> >>> Michael Horwitz wrote:
>> >>> >
>> >>> > Which version of AppFuse are you using and with which web front
>> end?
>> >>> > Details
>> >>> > as to which persistence layer you have chosen would also be useful:
>> >>> > Hibernate, JPA or iBatis?
>> >>> >
>> >>> > P.S. You English is excellent, by the way!
>> >>> >
>> >>> > Thanks
>> >>> >
>> >>> > Mike.
>> >>> >
>> >>> > On 7/13/07, Sielm <[EMAIL PROTECTED]> wrote:
>> >>> >>
>> >>> >>
>> >>> >> Hi all. I'm new to J2EE and AppFuse and have been tinkering with
>> it
>> >>> for
>> >>> a
>> >>> >> while now. The main thing is this: i have a "big" object called
>> >>> >> WeekActivity
>> >>> >> which represents an activity made by an user in a week, and i need
>> to
>> >>> be
>> >>> >> able to make a form to create new rows in the database for this
>> >>> object.
>> >>> >> This
>> >>> >> object has 3 properties (attributes), which are User, Week and
>> >>> Activity.
>> >>> >> Each of these three are beans which go into the database. This way
>> a
>> >>> >> WeekActivity row in the database would have the user id, the week
>> id,
>> >>> the
>> >>> >> activity id and the hibernate generated primary key ID. Roughly
>> this
>> >>> is
>> >>> >> the
>> >>> >> way this is mapped:
>> >>> >>
>> >>> >> · User: table user (ID, ... , other columns)
>> >>> >> · Week: table week (ID, ..., other columns)
>> >>> >> · Activity: table activity (ID, ..., other columns)
>> >>> >> · WeekActivity: table user_week_activity (user.id, week.id,
>> >>> activity.id
>> >>> ,
>> >>> >> ID)
>> >>> >> · method getUser ---> hibernate.many-to-one column="user.id"
>> >>> (because
>> >>> >> a
>> >>> >> user could have several activities in a week). I'm assuming this
>> will
>> >>> map
>> >>> >> the ID column of the User table to this table as a foreign key
>> (maybe
>> >>> i
>> >>> >> got
>> >>> >> this wrong in hibernate?)
>> >>> >> · method getWeek ---> same as above, a many to one for
>> week.id
>> >>> >> because
>> >>> >> a
>> >>> >> week can appear in several activities
>> >>> >> · method getActivity ---> hibernate.one-to-one, because an
>> >>> activity
>> >>> >> will
>> >>> >> have a unique ID always, for each time the form is used no matter
>> the
>> >>> >> user
>> >>> >> or the week.
>> >>> >>
>> >>> >>
>> >>> >> But User and Week are already in the database, so that in the
>> >>> >> weekActivityForm.jsp i just pass the user.id and the week.id as
>> >>> request
>> >>> >> parameters, but i need to create a new activity.id (and insert the
>> >>> new
>> >>> >> activity object into the database) each time the weekActivityForm
>> is
>> >>> >> filled
>> >>> >> and submitted, and i don't see the way to do that. At this moment
>> >>> when
>> >>> i
>> >>> >> submit the form hibernate complains about activity.id not having a
>> >>> >> default
>> >>> >> value, because i try to save the "big object" without setting the
>> >>> >> activity.id.
>> >>> >>
>> >>> >> In the WeekActivityFormController i have the method onSubmit which
>> >>> reads
>> >>> >> the
>> >>> >> week.id and user.id from the request, uses the manager to fetch
>> the
>> >>> Week
>> >>> >> and
>> >>> >> User objects from the database and sets them into the WeekActivity
>> >>> >> object.
>> >>> >> But what can i do to also set the Activity object? The thing is
>> that
>> >>> i
>> >>> >> need
>> >>> >> to insert a new Activity object into the database and then use the
>> >>> >> WeekActivity.setActivity(activity) method BUT that new activity
>> >>> object
>> >>> >> has
>> >>> >> a
>> >>> >> new generated ID each time and i don't know how to fetch it from
>> the
>> >>> >> onSubmit method...if i even can do that.
>> >>> >>
>> >>> >> I'm afraid i was a bit confusing explaining my problem...the short
>> >>> >> version
>> >>> >> would be: i have a commandobject in a form, and one of its
>> properties
>> >>> is
>> >>> >> another object which needs to be created each time i create a new
>> >>> >> instance
>> >>> >> of the commandobject (because this property should be always
>> unique)
>> >>> and
>> >>> >> when i try to persist the original commandobject i have no way of
>> >>> setting
>> >>> >> the property because i can't access the property id (which would
>> be
>> >>> >> generated when persisted too!)
>> >>> >>
>> >>> >> If someone understands my problem, i would be really grateful to
>> hear
>> >>> >> about
>> >>> >> it. And if someone doesn't but wants to help and need a more
>> indepth
>> >>> >> explain
>> >>> >> i would try my best. Thanks for your time and patience, and anyway
>> a
>> >>> big
>> >>> >> salute for everyone in the mailing list. Have a good day :)
>> >>> >>
>> >>> >> PS: sorry my English is a bit lacking ;)
>> >>> >> --
>> >>> >> View this message in context:
>> >>> >>
>> >>>
>> http://www.nabble.com/How-to-insert-a-new-object-from-another-object-%28command%29-form-tf4076716s2369.html#a11586895
>> >>> >> Sent from the AppFuse - User mailing list archive at Nabble.com.
>> >>> >>
>> >>> >>
>> ---------------------------------------------------------------------
>> >>> >> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> >>> >> For additional commands, e-mail: [EMAIL PROTECTED]
>> >>> >>
>> >>> >>
>> >>> >
>> >>> >
>> >>>
>> >>> --
>> >>> View this message in context:
>> >>>
>> http://www.nabble.com/How-to-insert-a-new-object-from-another-object-%28command%29-form-tf4076716s2369.html#a11593027
>> >>> Sent from the AppFuse - User mailing list archive at Nabble.com.
>> >>>
>> >>> ---------------------------------------------------------------------
>> >>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> >>> For additional commands, e-mail: [EMAIL PROTECTED]
>> >>>
>> >>>
>> >>
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/How-to-insert-a-new-object-from-another-object-%28command%29-form-tf4076716s2369.html#a11610981
>> Sent from the AppFuse - User mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>
>
--
View this message in context:
http://www.nabble.com/How-to-insert-a-new-object-from-another-object-%28command%29-form-tf4076716s2369.html#a11612220
Sent from the AppFuse - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]