Hi,
I have exactly the scenario as described in Chapter "7.6. More complex association mappings"
I have a historized table "leaf" which includes 4 columns "lid", "start", "end" and "parentid" where parentid points to another lid.
So the content could be something like this:
lid, start, end, parentid
1, 2006-09-01, 2006-09-02, NULL
1, 2006-09-02, NULL, NULL
2, 2006-09-02, NULL, 1
First I thought the problem described below is, that the same table/class is referenced inside the properties-node but even with duplicated tables/classes with different names, i get the problem that the generated SQL is not correct.
Does anybody have an idea, what I'm doing wrong ?
Thanks in advance,
Dirk
And here the code and error details:
Mapping documents:
Code:
<class name="sandbox.model.Leaf" table="leaf" entity-name="Leaf">
<id name="id" type="short" column="id" >
<generator class="native">
</generator>
</id>
<property name="start" column="start" type="timestamp" length="19" not-null="true" />
<property name="end" column="end" type="timestamp" length="19" not-null="false"/>
<many-to-one name="parent"
property-ref="customKey"
class="Leaf">
<column name="parentid"/>
<formula>'1'</formula>
</many-to-one>
<properties name="customKey">
<property name="lid" type="short" not-null="false" insert="false" update="false"/>
<property name="isCurrent" type="boolean">
<formula>case when end is null then 1 else 0 end</formula>
</property>
</properties>
</class>
Full stack trace of any exception that occurs:
org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not load an entity: [Leaf#component[lid,isCurrent]{lid=null, isCurrent=true}]; bad SQL grammar [select leaf0_.id as id0_0_, leaf0_.start as start0_0_, leaf0_.end as end0_0_, leaf0_.parentid as parentid0_0_, leaf0_.lid as lid0_0_, '1' as formula0_0_, case when end is null then 1 else 0 end as formula1_0_ from leaf leaf0_ where leaf0_.lid=? and leaf0_.null=?]; nested exception is java.sql.SQLException: Unknown column 'leaf0_.null' in 'where clause'
java.sql.SQLException:
Unknown column 'leaf0_.null' in 'where clause'
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2851)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1534)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1625)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2297)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2226)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1812)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1657)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
Name and version of the database you are using:
MySQL + Oracle (both with the same problem)
The generated SQL (show_sql=true):
select currentlea0_.id as id1_0_, currentlea0_.start as start1_0_, currentlea0_.lid as lid1_0_, currentlea0_.end as end1_0_, case when end is null then 1 else 0 end as formula1_0_ from currentleaf currentlea0_ where currentlea0_.lid=? and currentlea0_.null=?
The database creation script:
CREATE TABLE `leaf` (
`id` smallint(6) NOT NULL auto_increment,
`lid` smallint(6) NOT NULL,
`start` datetime NOT NULL,
`end` datetime default NULL,
`parentid` smallint(6) default NULL,
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;