I'm Using Hibernate 3.6.10 based JPA2.0 , I created two objects which are Many-to-Many relation.
@Entity
@Table(name="Project")
public class Project implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String topic;
@Lob
@Basic(fetch=FetchType.LAZY)
private String content;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="TrProj_Area",joinColumns=@JoinColumn(name="TrProj_ID"), inverseJoinColumns=@JoinColumn(name="Area_ID"))
private Set<Area> areas = new HashSet<Area>();
//getters and setters
}
@Entity
@Table(name="Area")
public class Area implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
@ManyToMany(mappedBy="areas")
private Set<Project> projects;
//getters and setters
}
The Project entity has a content property,It is Lob and Lazy Fetch, When I created a Query like this:
select distinct o from Project o join o.areas a where a.id in(?,?)
It works fine on MySQL 5.5,but SQLServer 2005 doesn't.SQLServer error log is below:
Error code 306, SQL state S0001: The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator
I known this issue is caused by the content property,it seems that the lazy fetch config doesn't work , the text column can not include in the select distinct statement, but I can't remove it from my Entity, how can I solve this issue?
Added at 20120722
I have trace the MySQL(mysql-connector-java-5.1.10-bin.jar as driver) generated SQL in hibernate, is below:
Hibernate:
select
distinct proj0_.id as id1_,
proj0_.content as content1_,
proj0_.topic as topic1_,
from
Project proj0_
inner join
Proj_Area areas1_
on proj0_.id=areas1_.Proj_ID
inner join
Area area2_
on areas1_.Area_ID=area2_.id
where
area2_.id in (
? , ?
)
order by
proj0_.id desc limit ?
and this is generated by SQLServer2005(sqljdbc4.jar as driver),I'm concerning the error is caused by the group by statement
Hibernate:
WITH query AS (select
ROW_NUMBER() OVER (
order by
proj0_.id desc) as __hibernate_row_nr__,
proj0_.id as id1_,
proj0_.content as content1_,
proj0_.topic as topic1_,
from
project proj0_
inner join
proj_area areas1_
on proj0_.id=areas1_.proj_id
inner join
area area2_
on areas1_.area_id=area2_.id
where
area2_.id in (? , ?))
group by
proj0_.id,
proj0_.content,
proj0_.topic) SELECT
*
FROM
query
WHERE
__hibernate_row_nr__ BETWEEN ? AND ?