JPA的新手。使用where子句构建select查询。我需要从表联系人中选择所有ContactName' s等于String名称的值。
使用以下代码创建数据库表:
CREATE TABLE Contacts (
ContactId BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
ContactName VARCHAR(100) NOT NULL,
ContactEmailID VARCHAR(100) NOT NULL,
UserName VARCHAR(100) NOT NULL,
INDEX Contact_Names (ContactName)
) ENGINE = InnoDB;
以下是我的实体类;
@Entity
private String UserName;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ContactId")
public long getContactId() {
return ContactId;
}
public void setContactId(long contactId) {
ContactId = contactId;
}
@Basic
@Column(name = "UserName")
public String getUserName() {
return UserName;
}
public void setUserName(String UserName) {
this.UserName = UserName;
}
以下是我的ContactServlet类代码,我正在尝试编写我的CriteriaBuilder代码。
EntityManager manager = null;
EntityTransaction transaction = null;
try{
manager = this.factory.createEntityManager();
transaction = manager.getTransaction();
transaction.begin();
CriteriaBuilder cb = manager.getCriteriaBuilder();
CriteriaQuery<Contact> q1 = cb.createQuery(Contact.class);
Root<Contact> postRoot = q1.from(Contact.class);
q1.select(postRoot).where(cb.equal(postRoot.get("UserName"), name));
TypedQuery<Contact> qry = manager.createQuery(q1);
List<Contact> result = qry.getResultList();
for (Contact contactInstance : result)
{
DBContactName = contactInstance.getContactName().trim();
DBContactEmail = contactInstance.getContactEmailID().trim();
.....
不知道我哪里出错了?
以下是执行项目时的错误: java.lang.IllegalArgumentException:无法解析org.hibernate.jpa.criteria.path.AbstractPathImpl.locateAttribute中org.hibernate.jpa.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:117)中路径的属性[UserName] (AbstractPathImpl.java:214)位于com.ContactServlet.doPost(ContactServlet.java:110)的org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:185),位于javax.servlet.http.HttpServlet。服务(HttpServlet.java:644)位于org.apache.catalina的org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)的javax.servlet.http.HttpServlet.service(HttpServlet.java:725) .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java) :239)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java) :206)org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)位于org.apache.catalina.authenticator的org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)。 AuthenticatorBase.invoke(AuthenticatorBase.java:505)org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)at org org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)的orap.apache.catalina.connector.CoyoteAdapter.service。 .java:534)org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:658)org.apache.coyote。 org.apache上的http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:222) .tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1566)atg.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1523)at java.util.concurrent.ThreadPoolExecutor .runWorker(ThreadPoolExecutor.java:1142)at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61 )在java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:1)
尝试:
q1.select(postRoot).where(cb.equal(postRoot.get("userName"), name));
小写的UserName属性的 u 。