I've a problem with mysql and ejb3ql query on an entity bean with a composite key.
I didn't have problems with the tables with a normal primary key.
------entity------
package myexample;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.IdClass;
import javax.persistence.Table;
/**
* @author Dani
* Creation date: 16-9-2007
*/
@Entity
@Table(name="permission")
@IdClass(webalbum.PermBeanPk.class)
public class PermBean
{
@javax.persistence.Id
@Column(name="type")
private String type;
@javax.persistence.Id
@Column(name="param")
private String param;
@Column(name="ok")
private boolean ok;
public PermBean() { };
public PermBean(String type, String param,boolean ok) {
this.type=type;
this.param=param;
this.ok = ok;
}
public boolean isOk() {
return ok;
}
public void setOk(boolean ok) {
this.ok = ok;
}
public boolean equals(Object obj)
{
if(obj == null || obj.getClass() != this.getClass())
return false;
if ( this == obj ) return true;
if (this.getType().equals(((PermBean)obj).getType()) && this.getParam().equals(((PermBean)obj).getParam()))
return true;
return false;
}
public int hashCode() {
return 7*this.getType().hashCode()+9*this.getParam().hashCode();
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
package myexample;
import javax.persistence.Column;
/**
* @author Dani
* Creation date: 16-9-2007
*/
public class PermBeanPk implements java.io.Serializable
{
@Column(name="type")
private String type;
@Column(name="param")
private String param;
public PermBeanPk()
{ }
public PermBeanPk(String type, String param)
{
this.type=type;
this.param=param;
}
public boolean equals(Object obj)
{
if(obj == null || obj.getClass() != this.getClass())
return false;
if ( this == obj ) return true;
if (this.type.equals(((PermBeanPk)obj).getType()) && this.param.equals(((PermBeanPk)obj).getParam()))
return true;
return false;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
public int hashCode() {
return 7*type.hashCode()+9*param.hashCode();
}
}
------persistence.xml------
<persistence >
<persistence-unit name="myexampleUN">
<jta-data-source>java:/MySqlDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value=""/> <!-- I don't want automatically created tables-->
</properties>
</persistence-unit>
</persistence>
My tables are mySql InnoDBD type, so I've also used the following values of hibernate.dialect
org.hibernate.dialect.MySQLDialect
org.hibernate.dialect.MySQLInnoDBDialect
org.hibernate.dialect.MySQLMyISAMDialect
------query------
package ejb.session;
...import....
@Stateless (name = "MySessionEjb")
public class ManagerBean implements Manager
{
@PersistenceContext (unitName="myexampleUN")EntityManager em;
...
...
public void getPermission(String type, String param)
{ PermBean pe=null;
Query query = em.createQuery( "SELECT pb FROM PermBean pb WHERE pb.type = :p1 AND pb.param = :p2" );
query.setParameter( "p1", type );
query.setParameter( "p2", param );
try
{
pe=(PermBean)query.getSingleResult();
}
catch( javax.persistence.NoResultException e ){e.printStackTrace(); }
System.out.println("getPermission("+type+", "+param+")= "+pe.isOk());
}
------exception------
....
....
17:56:54,324 WARN JDBCExceptionReporter:77 - SQL Error: 1064, SQLState: 42000
17:56:54,324 ERROR JDBCExceptionReporter:78 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'permission permbean0_ where permbean0_.type='valid' and permbean0_.param='a' at line 1
....
....
I've tryed all possible annotations on entity and all version of mySql jdbc driver, but it doesn't work either.
It works only if I use createNativeQuery(....) with mySql's native sql.
|