Now() in ColdFusion (Railo) ORM entity property as a default? - orm

A common design I use is to set a date column with the current date as the default. For SQL Server I use getDate() and for MySQL now() or current_timestamp.
Implementing a MySQL solution in ORM, seemed the way to do it would be:
property name="dtSaved" ormtype="date" dbdefault="now()";
However, this isn't working, but isn't throwing an error either. When I run ORMReload(), it seems to get stuck on this table, and none of the entities that come after (alphabetically) get created. (I'm using dbcreate="dropcreate")
Note that this is Railo 3.3.1, not Adobe ColdFusion 9.

You can set a dynamic default value in your constructor, so something like this:
component persistent="true" {
property name="measurementDate" ormtype="date";
function any init(){
if (IsNull(variables.measurementDate)){
variables.measurementDate = Now();
}
return this;
}
}
Comment by John Whish – November 22, 2010
from:
http://www.aliaspooryorik.com/blog/index.cfm/e/posts.details/post/coldfusion-9-orm-example-215

Related

Open CMIS - Querying string property results in weird behavior

I'm executing the following SQL query:
SELECT doc.cmis:description, doc.cmis:name
FROM cmis:document doc
WHERE IN_FOLDER(doc,'folderID')
This result in something like below:
doc.cmis:description = "this is description"
doc.cmis:name = "fileName"
Now, if I add following statements, it returns zero result:
and doc.cmis:description = 'this is description'
However, if I modify and-statement with following, it works:
and doc.cmis:description like '%'
If I add one character (but not two interestingly...) as below, it also works:
and doc.cmis:description like '%t%'
It's very interesting to note that and-statement work very well with doc.cmis:name (as well as other properties).
Does anyone have clue as to why this strange / mysterious behavior is occurring?
The specifications delegate to the implementer if the cmis:description is queryable or not.
Anyway, which Alfresco version are you using ? There was an issue/bug time ago, but this should be solved: The cmis:description field should be queryable, although I don't know if it's fixed in enterprise or community.
By the way, I am currently using Alfresco Community 4.2.f and I have the same problem.

From within a grails HQL, how would I use a (non-aggregate) Oracle function?

If I were retrieving the data I wanted from a plain sql query, the following would suffice:
select * from stvterm where stvterm_code > TT_STUDENT.STU_GENERAL.F_Get_Current_term()
I have a grails domain set up correctly for this table, and I can run the following code successfully:
def a = SaturnStvterm.findAll("from SaturnStvterm as s where id > 201797") as JSON
a.render(response)
return false
In other words, I can hardcode in the results from the Oracle function and have the HQL run correctly, but it chokes any way that I can figure to try it with the function. I have read through some of the documentation on Hibernate about using procs and functions, but I'm having trouble making much sense of it. Can anyone give me a hint as to the proper way to handle this?
Also, since I think it is probably relevant, there aren't any synonyms in place that would allow the function to be called without qualifying it as schema.package.function(). I'm sure that'll make things more difficult. This is all for Grails 1.3.7, though I could use a later version if needed.
To call a function in HQL, the SQL dialect must be aware of it. You can add your function at runtime in BootStrap.groovy like this:
import org.hibernate.dialect.function.SQLFunctionTemplate
import org.hibernate.Hibernate
def dialect = applicationContext.sessionFactory.dialect
def getCurrentTerm = new SQLFunctionTemplate(Hibernate.INTEGER, "TT_STUDENT.STU_GENERAL.F_Get_Current_term()")
dialect.registerFunction('F_Get_Current_term', getCurrentTerm)
Once registered, you should be able to call the function in your queries:
def a = SaturnStvterm.findAll("from SaturnStvterm as s where id > TT_STUDENT.STU_GENERAL.F_Get_Current_term()")

nHibernate 3.0 queries

Working through the summer of nHibernate tutorials have gotten to the section on queries. Seems there have been changes since that series was made. So I went to the online docs for nHB 3.0 but code such as:
IList cats = session.CreateCriteria(typeof(Cat))
.Add(Expression.Like("Name", "Fritz%"))
.Add(Expression.Between("Weight", minWeight, maxWeight))
.List();
Generates the error "The name 'Expression' does not exist in the current context"
Code like:
return session.CreateCriteria(typeof(DataTransfer.Customer))
.Add(new NHibernate.Criterion.LikeExpression("Firstname", firstname))
.Add(new NHibernate.Criterion.LikeExpression("Lastname", lastname))
.List<Customer>();
Works but it seems that it is missing a number of query methods like GtExpression.
Are the online docs up to date, and if so, why can't I use Expression...
If the online docs aren't up to date then where do I get a description of the Criterion interface?
Thanks
You forgot to add using NHibernate.Criterion;.
Anyway, the Expression class is deprecated. Use Restrictions instead.
Weird thing. I still use Expression.* static methods and these are still work. Are you sure you use the latest version of NH3.0? I use Alpha 2 version.
If you need to make it work urgently, let's try the QueryOver<> feature:
return session.QueryOver<DataTransfer.Customer>()
.WhereRestrictionOn(u => u.Name).IsLike("Fritz%")
.AndRestrictionOn(u => u.Weight).IsBetween(minWeight).And(maxWeight)
.List();
It works well for simple queries

NHibernate 2.1.2 throwing DateTime null overflow exception

I'm having a similar problem to these questions:
NHibernate 2.* mapping files: how to define nullable DateTime type (DateTime?)?
NHibernate won't persist DateTime SqlDateTime overflow
I'm using NHibernate 2.1.2 and FluentNhibernate 1.0.0.636. With NHibernate 2.x the nullable DateTime? issue should be fixed and I shouldn't have to do anything special with my mapping. Accordingly, all my DateTime properties are simply set like so:
public virtual DateTime? CreatedOn { get; set; }
In my database (SQL2008), all DateTime properties are set to allow null. I have my NHibernate config file setup to use the SQL2008 dialect:
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
NHibernate works fine for anything that does not include a DateTime. For reference, here's the exact error I'm receiving:
> at
> NHibernate.AdoNet.SqlClientSqlCommandSet.ExecuteNonQuery()</StackTrace><ExceptionString>System.Data.SqlTypes.SqlTypeException:
> SqlDateTime overflow. Must be between
> 1/1/1753 12:00:00 AM and 12/31/9999
> 11:59:59 PM.
If I run SQL Profiler, I can see the last command that NHibernate attempts to execute (this was a very long statement, so I've truncated it):
exec sp_executesql N'UPDATE Projects SET Job = #p0, CreatedOn = #p1, .. WHERE (Where Clause), #p0=219221, #p1=NULL
If I execute this statement as a query, SQL persists it fine, doesn't complain at all!
What's going on?
Your DateTime properties are probably set to DateTime.MinValue (1/1/0001) instead of null or a value in the valid range for a DateTime column.
I don't know why, but this works here. Unlike the example in the link, mine are DateTime? so .... I do not know why it works, but it does.
I had this same issue. At first I thought I fixed it by switching my sql type from a nullable date to a nullable datetime. However, that was a mirage. The root cause was a cascading update on another newly added table.
This would have been quickly resolved if everyone (but especially MS) threw more descriptive errors. Cest la vie.

How do I use SQL's GETDATE() and DATEADD() in a Linq to SQL expression?

If I have a Linq to SQL expression like this:
from subscription in dbContext.Subscriptions
where subscription.Expires > DateTime.Now
select subscription
I want this to to use the SQL Servers GETDATE() function instead of the time of the machine running the C# program.
The next question would be how to translate this:
DateTime.Now.AddDays(2)
to this:
DATEADD(dd, 2, GETDATE())
Try this:
[Function(Name="GetDate", IsComposable=true)]
public DateTime GetSystemDate()
{
MethodInfo mi = MethodBase.GetCurrentMethod() as MethodInfo;
return (DateTime)this.ExecuteMethodCall(this, mi, new object[]{}).ReturnValue;
}
EDIT: this needs to be a part of your DataContext class.
Now you can use GetSystemDate() instead of DateTime.Now in your queries.
As for date differences take a look at System.Data.Linq.SqlClient namespace, especially DayDiffXXX functions of SqlMethods class.
If you don't mind querying the database before every use, I would suggest the following workaround: Use ExecuteQuery in one place to get the date in the data context like this:
public partial class YourDataContext
{
public DateTime GetDate()
{
return ExecuteQuery<DateTime>("SELECT GETDATE()").First();
}
}
and then you can write
from subscription in dbContext.Subscriptions
where subscription > dbContext.GetDate().AddDays(2)
select subscription
You could use the ExecuteQuery to gain full control of the sql http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part-8-executing-custom-sql-expressions.aspx
I know it seems like very little gain (or perhaps on the contrairy) over ADO.NET though.