BUG: Composite Primary Key
lihuoming Apr 12, 2007 9:29 AMI'm running Jboss4.2.0
Composite Primary Key Class:
package com.foshanshop.ejb3.bean; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Embeddable; @SuppressWarnings("serial") @Embeddable public class AirtLinePK implements Serializable { private String leavecity; private String arrivecity; public AirtLinePK(){} public AirtLinePK(String leavecity, String arrivecity) { this.leavecity = leavecity; this.arrivecity = arrivecity; } @Column(nullable=false,length=3,name="LEAVECITY") public String getLeavecity() { return leavecity; } public void setLeavecity(String leavecity) { this.leavecity = leavecity; } @Column(nullable=false,length=3,name="ARRIVECITY") public String getArrivecity() { return arrivecity; } public void setArrivecity(String arrivecity) { this.arrivecity = arrivecity; } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof AirtLinePK)) return false; final AirtLinePK airtLinePK = (AirtLinePK) o; if (!leavecity.equals(airtLinePK.getLeavecity())) return false; if (!arrivecity.equals(airtLinePK.getArrivecity())) return false; return true; } public int hashCode() { int result; result = leavecity.hashCode(); result = 29 * result + arrivecity.hashCode(); return result; } }
package com.foshanshop.ejb3.bean; import java.io.Serializable; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.OneToMany; import javax.persistence.OrderBy; import javax.persistence.Table; @SuppressWarnings("serial") @Entity @Table(name = "AirLine") public class AirLine implements Serializable { private AirtLinePK id; private Boolean onoff; public AirLine(){} public AirLine(AirtLinePK id, Boolean onoff){ this.id = id; this.onoff = onoff; } @EmbeddedId public AirtLinePK getId() { return id; } public void setId(AirtLinePK id) { this.id = id; } public Boolean getOnoff() { return onoff; } public void setOnoff(Boolean onoff) { this.onoff = onoff; } }
Session bean:
package com.foshanshop.ejb3.impl; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import com.foshanshop.ejb3.AirLineDAO; import com.foshanshop.ejb3.bean.AirLine; import com.foshanshop.ejb3.bean.AirtLinePK; @Stateless @Remote ({AirLineDAO.class}) public class AirLineDAOBean implements AirLineDAO { @PersistenceContext protected EntityManager em; public Long getAirLineTotal() { Query query = em.createQuery("select count(a) from AirLine a where a.id.leavecity =?1 and a.id.arrivecity=?2"); query.setParameter(1, "PEK"); query.setParameter(2, "CAN"); return query.getSingleResult(); } }
Jboss Log print Error:
2007-04-12 21:03:24,031 DEBUG [org.hibernate.util.JDBCExceptionReporter] could not execute query [select count((airline0_.LEAVECITY, airline0_.ARRIVECITY)) as col_0_0_ from AirLine airline0_ where airline0_.LEAVECITY=? and airline0_.ARRIVECITY=?]
java.sql.SQLException: Operand should contain 1 column(s)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1695)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3004)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1128)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1222)
above code run in Toplink, it work fine
I am using following code in jboss,it work fine:
Query query = em.createQuery("select count(a.id.leavecity) from AirLine a where a.id.leavecity =?1 and a.id.arrivecity=?2");