I yould like to know if it is possible to directly save an object in a hibernate/spring/maven project with path. So my class has an object attribute "Category". In the form I have a select. If I put different Category objects as Value in select and I link the select with form:select and path to the Category attribute, will it save it automatically ? Otherwise I would have to get the ID, get back the object from the ID in my controller and then set the Category attribute with the setter in order to save the whole thing.
Thanks
Related
I have used struts2 tags for getting action class property values in jsp page.I have a table called employee.In that employee table, address is the foriegn key.I get a employee details in lazy fetcthing and load the values using list.
List<Employee> empList=cr.list();
when i get the address from struts2 tags in jsp page using the below tag.
<s:property value="address.getDoorno()"/>
i know in struts2 the action class properties are stored on the valuestack.
my doubt is the valuestack stores the proxy object or original object and if stores the proxy object database hit occurs or not,and if original object when value stack converts the proxy object to original object?
Any help will be greatly appreciated!!
As you have configured lazy loading struts 2 will store proxy object. Either you enable eager loading or initialize the address object within session itself
Well, I changed my application FlushMode from AUTO to COMMIT, because i don't want to update or insert new objects from dirty-check hibernate.
I will try explain my problem with a scenario:
I have a ManagedBean (CustomerMB) linked with a XHTML (customer.xhtml) page, this ManagedBean is "ViewScoped" and have a object called "bean" (Customer.class type).
User starts to change information about Customer (like name, age, address and others), and this values are set into "bean" object. But for some unknown reason, user decide to click "F5" (refresh browser) and ViewScoped is destroyed and another is created (i think so).
In this moment i expect that all informations about "Customer" was lost (this is correct for me) and user must start again the changes of this Customer. But the opposite happens, the "bean" object continue with all information in cache, and if i change my XHTML page to another managedBean the "bean" object also continue in cache and i don't want this.
I tried to set "bean = null" in my ManagedBean but when i do a "SELECT * FROM Customer c WHERE c.id = :id" the "customer" object is returned with new value (typed by user) and not equal Database as i expected, i don't know why.
I would like to know if it's posible to change the insert and update attributes of a property defined in the mapping of a Class.
This is because in one scenario I need to update a property (or properties), but not in another, it's posible?
Thanks in advance
EDIT: Lets say that I've the Class User(with name, surname and loginDate), when the user logs into the app, I need to update only loginDate. But the administrator of the system must be able to edit the name and the surname of the User.
The only other solution that ocurrs to me is to use HQL for a Update (or in the worst case SQL), but I want if it's posible to modify that attributes.
EDIT 2: after reading Java persistence with hibernate and some forum threads I found that once the sessionFactory is created the mappings are immutables, and though You can change the properties programmatically, You need to create a new sessionFactory
// this is what the login screen calls
void updateLoginDate(Date date)
{
User user = session.get(User.class);
user.setDate(date);
session.Flush();
}
and in the mapping you could specify dynamicUpdate = true on the class so that the generated sql only updates columns which have changed
I have a form to add new Appartments, and in this form I have a dropdown where the user can choose what Person is responsible.
Apparantly my application thinks that the Person is modified when you select from the dropdown and try to save the Appartment. And it gives me the error below indicating i should save the Person first. But the Person is not modified. It is only the Appartment that should be saved with a reference to a different Person.
object is an unsaved transient instance - save the transient instance before merging
How can I make my application understand that the Person himself has not been modified. Only the Appartment?
Here is my code:
#Entity
#Table(name = "Person")
public class Person{
#Id
private Long id;
private String fullName;
....
}
#Entity
#Table(name = "Appartment")
public class Appartment{
....
#ManyToOne (fetch = FetchType.LAZY)
#JoinColumn (name = "client_contact_person_id")
private Person clientResponsiblePerson;
}
The action loads all Persons into a List responsiblePersons.
And the JSP:
<s:select name="appartment.clientResponsiblePerson.id" list="responsiblePersons"
listKey="id" listValue="fullName" headerKey="-1"
label="%{getText('appartment.clientContact.ourContact')}" headerValue="%{getText('person.choose')}"
required="true" cssClass="select medium" />
Any ideas? I've been searching and debugging for hours without any solution... :(
UPDATE:
Steven suggested that i remove the id from appartment.clientResponsiblePerson.id. This is a reasonable suggestion. I did just try it, but then it seems like my Application dont know how to map the value submitted by the form to a Person-object. As im setting listKey="id" the value submitted is the Person's ID.
I recieve the following errors:
Invalid field value for field "appartment.clientResponsiblePerson".
tag 'select', field 'list', name 'appartment.clientResponsiblePerson': The requested list key 'responsiblePeople' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
So my initial thought was that maybe i should delete the listKey and listValue from my s:select. Maybe struts automagically detects the id from the object and uses toString for value? But I tried this as well without any more luck.
Another really strange thing is that I do the exact same thing in another form. In that form i am selecting Areas from a dropdown. And I am using appartment.area.id for name. And it works perfectly there. Strange.. I also checked that the Area - Appartment reference was not set up to automaticly persist or merge.
It strikes me that what i am trying to achive should be really straigh forward. What is it that i am not getting here?
Apparantly my application thinks that the Person is modified when you select from the dropdown and try to save the Appartment.
That's exactly what your code is doing. The following line is the culprit:
appartment.clientResponsiblePerson.id
That is telling the Struts2 framework to take the id of the person you selected in your drop down and pass it to getAppartment().getClientResponsiblePerson().setId(id). That doesn't change to a new responsible person, it changes the primary key for the existing person. Calling setClientResponsiblePerson(Person) would change the person.
Try using appartment.clientResponsiblePerson instead and see how that works for you.
Update
Another really strange thing is that I do the exact same thing in another form.
I don't see how that would work either.
Struts2 doesn't know what a Person is, so you have a few options:
Create a type converter to tell Struts2 how to convert from "1" (or whatever is passed in from your dropdown) to an instance of a Person.
Add a setPerson(Integer) method on your action which will look up the appropriate Person entity based on the Integer primary key passed in and then update your s:select to <s:select name="person" list="responsiblePersons" .../>
Personally, I use #1.
I am attempting to create a custom attribute that can be assigned to an existing Active Directory user in my domain. I am not fully aware of how to achieve this. It is my understanding that once the attribute has been created, I can assign it to the user via:
mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("attributeName", "attributeValue"))
ctx.modifyAttributes(userDN, mods)
Any information is appreciated.
Not sure what you want to do.
But Active-Directory is a Directory, so it use a SCHEMA to define which attributes can be used in an object. This means that you can modify (add, delete, replace) the value of an attribut that exists (in the SCHEMA) for a given class, but can'nt add a custom attribut to a class without modifying the SCHEMA.