I'm working on a project using spring hibernate struts2 framework and MySQL database, I have a problem when I wanted to add a field of type Date
does not work, it means when I try to insert a Date field, it sends me this error:
WARNING: Error setting value
ognl.MethodFailedException: Method "setDATE_PANNE" failed for object model.Panne # 1d055e2 [java.lang.NoSuchMethodException: setDATE_PANNE ([Ljava.lang.String;)]
/ - Encapsulated exception ------------ \
java.lang.NoSuchMethodException: setDATE_PANNE ([Ljava.lang.String;)
NB: (DATE_PANNE -> this is the date fields) and setDATE_PANNE is the setter for this attribute. DATE_PANNE is of type date
apparently the problem is that struts2 has taken my Date as a String thing that he should not do.
I do not know how to convert and where?
I have recently been working with struts 2 and have run into this ognl Exception a few times. It seems to happen when you have a control on a JSP, such as an input checkbox named "checkBox", and a method from your controller (java class, not JSP) similarly named "setCheckBox()". Somehow, behind the scenes, struts 2 or Ognl is generating a naming conflict and is unable to execute the correct method. Simply renaming either the control on the JSP (from my example from "checkBox" to "checkBox1") or renaming the method from the controller should solve your issue. Let me know if I did not elaborate enough. Hope this helps.
Regards,
Nick
Related
I'd like to use a CSV file as a lookup table to update some attributes.
So I figured the LookUpAttribute processor was what I needed. I configured it with as SimpleCsvFileLookupService as the Lookup Service, but I can't get it to work yet.
My SimpleCsvFileLookupService is configured but stays in a "enabling" state, and the LookUpAttribute processor still tells me it's "invalid because performing validation depends on referencing a Controller Service that is currently disabled".
I dont understand why it doesn't enable. Has somebody used these components ? Thx
Edit :
I didn't see the message in the left. It says the mapping for "1" is not found ("1" is set as the lookup key column and in the csv the header row is "1;2;3;4;5;6;7;8".
What am I missing ? I can't find any explanation as to how to use this controller service.
Edit2 : The SimpleCsvFileLookupService properties
Edit3 : Extract of the csv file
I have a Spring / Hibernate project and I am trying to store a date into the database but it's not working. It must be something stupid but I have no idea what I am doing wrong.
Here is my code:
user.setFailedPasswordAnswerAttemptCount(0);
user.setLastLoginDate(new Date());
user.setIsOnline(true);
The other two variables (failedPasswordAnswerAttemptCount and isOnline) are getting written to the database without issue. I have also tried it with just passing a java.util.Date instead of a java.sql.Timestamp...same result. Here is how the property is defined on the user object:
private Date lastLoginDate;
#Temporal(TemporalType.TIMESTAMP)
#Column(name="last_login_date")
public Date getLastLoginDate() {
return this.lastLoginDate;
}
public void setLastLoginDate(Date lastLoginDate) {
this.lastLoginDate = lastLoginDate;
}
Here is the column definition:
`last_login_date` datetime DEFAULT NULL
Any help? I don't even know what else to look for as this should be working.
Some more detail about the error: No errors or strange messages in the hibernate log. The hibernate log is showing a parameterized query but it isn't telling me what it is actually writing. It looks like it's not updating the column at all. In other words, if there is already a date there it doesn't change, or if it is null it doesn't change.
Update: I have looked at the logs and it looks like hibernate does write the proper data, but then immediately writes the incorrect data again. I see the following entry in the log:
11:15:12.280 [http-bio-8080-exec-26] TRACE o.h.e.def.AbstractSaveEventListener - detached instance of: com.hi.model.User
11:15:12.280 [http-bio-8080-exec-26] TRACE o.h.e.def.DefaultMergeEventListener - merging detached instance
And right after that I see it putting the old value back in for the lastLoginDate.
Why are you using
Date date = new Date();
user.setLastLoginDate(new Timestamp(date.getTime()));
and not just this?
user.setLastLoginDate(new Date());
First - You may not want to use Date and Timestamp at the same time.(e.g. for collections, etc)
There are some classes in the Java platform libraries that do extend an instantiable
class and add a value component. For example, java.sql.Timestamp
extends java.util.Date and adds a nanoseconds field. The equals implementation
for Timestamp does violate symmetry and can cause erratic behavior if
Timestamp and Date objects are used in the same collection or are otherwise intermixed.
The Timestamp class has a disclaimer cautioning programmers against
mixing dates and timestamps. While you won’t get into trouble as long as you
keep them separate, there’s nothing to prevent you from mixing them, and the
resulting errors can be hard to debug. This behavior of the Timestamp class was a
mistake and should not be emulated. (Bloch, Effective Java, 2nd Ed.)
Second - I checked your examples, and it works fine for me on mysql-connector(5.1.21) / hibernate (4.0.1)
I prepared simple test project with arquillian integration test(You need to prepare jboss before running it):
https://github.com/rchukh/StackOverflowTests/tree/master/13803848
If you can provide some more information it might help - hibernate version, mysql version, mysql engine(MyISAM, InnoDB, etc.)
Otherwise it is possible that this is just a misconfiguration.
I found the problem. I am refactoring some code and it looks like I was doing this:
//get user object
User user = getUser();
//call a function which modifies user
functionModifiesUser();
//modify user
user.blah = blah;
entityManager.merge(user);
So the parent function had a stale copy of the user object when I tried to save it. Actually, removing the merge statement was enough to fix it. But I have refactored the code to put all this in one place.
Setting the column last_login_date as timestamp should work, at least works for me.
I'm using Spring Roo on top of MySQL. I pull dates out via the Roo-generated JSON methods, make changes to that data in a browser form, and save it back via the Roo-generated JSON methods.
The date format I'm getting out is yyyy-MM-dd, standard MySQL date format. I'm using a calendaring widget on the browser to ensure the date I'm submitting is the same format.
Unfortunately my data doesn't go right through the ...FromJson() method, failing with the error:
Parsing date 2007-12-12 was not recognized as a date format
I presume that the problem is that it's coming out as a string, but JPA feels like it needs to generate a Date object to update.
I'll happily show my code about this, but it's nothing Roo didn't build for me.
It occurs to me that there's something it's referring to when it says "recognized as a date format". Is there somewhere I can change what date formats it knows?
EDIT: With #nowaq's help, here's the ultimate answer:
public static Lease fromJsonToLease(String json) {
return new JSONDeserializer<Lease>()
.use(null, Lease.class)
.use(Date.class, new DateFormatter("yyyy-MM-dd"))
.deserialize(json);
}
That way JSONDeserializer knows what class it's dealing with AND builds a formatter for all the dates in that class. Wicked!
Your question is very related to this one: Spring-roo REST JSON controllers mangle date fields Take a look and make sure you're using correct DateTrasformer(s) with your JSON deserializers. E.g.
new JSONDeserializer()
.use(Date.class, new DateTransformer("yyyy-MM-dd") )
.deserialize( people );
I'm trying to return a formatted date from a razor template in umbraco. I'm not sure how to get a value from a field defined in a parameter though.
Here is the code I'm playing with. The field I'm passing in is called "articleDate". I'm getting the parameter value output, however when I try to get the value of the field using the parameter name it returns nothing. If I ask for the value by the field name itself, that works. How can I create a generic macro like this?
#{var param = #Parameter.dateField;}
Field Name: #param
<br/>
Field Value: #Model.param
<br/>
Field Value: #Model.articleDate
I tried using #Model.GetDynamicMember(..) as well, but that just throws an exception.
Field Value: #Model.GetDynamicMember("articleDate");
Error loading Razor Script getDate.cshtml
Cannot invoke a non-delegate type
Can someone point me in the right direction? I'm just trying to create a simple macro I can use to format dates across my page.
Is it possible to pass the value of my date directly into the razor macro? This is how I'm currently calling it:
<umbraco:Macro ID="Macro1" Alias="getDate" dateField="articleDate" runat="server"></umbraco:Macro>
If you call your Macro like you wrote:
<umbraco:Macro ID="Macro1" Alias="getDate" dateField="articleDate" runat="server"></umbraco:Macro>
You're actually passing the name of the field "articleDate". In the macro, you then may get the value of the Model's articleDate property by using:
Field Value: #Model.getProperty(Parameter.dateField).Value
Instead of macro's I also recommend using helpers or - for more complex scripts - RenderPage. A nice writeup can be found here:
http://joeriks.wordpress.com/2011/03/11/better-structure-for-your-razor-scripts-with-renderpage-in-umbraco/
Example:
#helper GetDate(dynamic dateField)
{
#dateField.ToString("[yourFormat]")
}
You may pass parameters to scripts rendered with RenderPage by using the Page object:
<umbraco:macro language="razor" runat="server">
#{
Page.dateField=Model.articleDate;
}
#RenderPage("~/macroscripts/getdate.cshtml")
</umbraco:macro>
getdate.cshtml:
#Page.datefield.ToString("[yourFormat]")
I am trying to populate a dropdown list control on an web page using VS 2008 and keep getting an error that it can't load the DataContext. My backend is a SQLx server 2005 DB. I create a Link To SQL data context and have 1 table in it. My LinKDataSource is as follows -
asp:LinqDataSource ID="LinqDataSource1" runat="server"ContextTypeName="DACDataContext" TableName="portfolio">
/asp:LinqDataSource
My dropdown definition is:
asp:DropDownList ID="ddlPortfolio" runat="server" Width="165px" DataSourceid="LinqDataSource1" DataTextField="porfolio_name"
DataValueField="portfolio_id">
/asp:DropDownList
I can see in my properties of my DatContext that the ContextTypeName is DACDataContext
The pecific errors I get are:
HttpException (0x80004005): Could not load type 'DACDataContext'.
and
InvalidOperationException: Could not find the type specified in the ContextTypeName property of LinqDataSource 'LinqDataSource1'.]
I know this must be something really stupid but I am at my wits end.
Please help.
Verify that DACDataContext is public and you may also try fully qualifying the type (ie. ContextTypeName="MyNamespace.DACDataContext")