我尝试查询连接拖表客户和主订单表。当我总是查询错误
-Customer.java
package com.mdr.model;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
//import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
*
* @author Sothon
*
*/
@Entity
@Table(name = "customer")
public class Customer{
@Id
@GeneratedValue
@Column(name="id")
private long id;
@Column(name="name")
private String name;
@Column(name="person")
private String person;
@Column(name="mail")
private String mail;
private int packFlg;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
@OneToMany
@JoinColumn(name="customerId",referencedColumnName="customer_id")
private List<MainOrder> mainOrder;
public List<MainOrder> getMaiOrder() {
return mainOrder;
}
public void setMaiOrder(List<MainOrder> mainOrder) {
this.mainOrder = mainOrder;
}
}
-MainOrder.java
package com.mdr.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="main_order")
public class MainOrder{
@Id
@GeneratedValue
@Column(name="id")
private long id;
@Column(name="file_dir")
private String fileDir;
@Column(name="date")
private String date;
@Column(name="customer_id")
private long customerId;
@Column(name="quantity")
/** getter and setter */
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFileDir() {
return fileDir;
}
public void setFileDir(String fileDir) {
this.fileDir = fileDir;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public long getCustomerId() {
return customerId;
}
public void setCustomerId(long customerId) {
this.customerId = customerId;
}
@ManyToOne
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
我想要来自表客户的查询电子邮件。我尝试这样的查询
public String getCustomerEmail(){
Session session = HibernateSessionFactory.getSession();
//Session session = sessionFactory.openSession();
String maxOrderNoDay = "";
try{
maxOrderNoDay = (String) session.createQuery("select mail from Customer cus join cus.MainOrder man WHERE man.id = 1").uniqueResult();
}catch(HibernateException e){
e.printStackTrace();
}finally{
session.close();
return maxOrderNoDay;
}
}
它总是显示像这样的错误
java.lang.NullPointerException
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1456)
at org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:864)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:779)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at com.mdr.dao.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:32)
at com.mdr.dao.MainOrderDaoImpl.getCustomerEmail(MainOrderDaoImpl.java:104)
at com.mdr.dao.TestDao.main(TestDao.java:138)
%%%% Error Creating SessionFactory %%%%
org.hibernate.DuplicateMappingException: Duplicate collection role mapping com.mdr.model.Customer.mainOrder
at org.hibernate.cfg.Configuration$MappingsImpl.addCollection(Configuration.java:2859)
at org.hibernate.cfg.annotations.CollectionBinder.bind(CollectionBinder.java:527)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2156)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at com.mdr.dao.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:71)
at com.mdr.dao.HibernateSessionFactory.getSession(HibernateSessionFactory.java:54)
at com.mdr.dao.MainOrderDaoImpl.getCustomerEmail(MainOrderDaoImpl.java:104)
at com.mdr.dao.TestDao.main(TestDao.java:138)
Exception in thread "main" java.lang.NullPointerException
at com.mdr.dao.MainOrderDaoImpl.getCustomerEmail(MainOrderDaoImpl.java:113)
at com.mdr.dao.TestDao.main(TestDao.java:138)
-hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="javax.persistence.validation.mode">none</property>
<!-- Database connection settings -->
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<!--
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<!-- Mapping (Map the Bean Class to Table in Database) -->
<mapping class="com.mdr.model.Customer" />
<mapping class="com.mdr.model.MainOrder" />
</session-factory>
</hibernate-configuration>
请帮助我!
答案 0 :(得分:2)
在customer.java
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@Column(name = "person")
private String person;
@Column(name = "mail")
private String mail;
private int packFlg;
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
private List<MainOrder> mainOrder;
// generate getter and setter here
}
班级MainOrder.java
@Entity
@Table(name = "main_order")
public class MainOrder {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Column(name = "file_dir")
private String fileDir;
@Column(name = "date")
private String date;
@ManyToOne
@JoinColumn(name = "customer_id", nullable = false)
private Customer customer;
// generate getter and setter here
}
您收到电子邮件的方法,
public String getCustomerEmail(){
Session session = HibernateSessionFactory.getSession();
//Session session = sessionFactory.openSession();
String maxOrderNoDay = "";
try{
maxOrderNoDay = (String) session.createQuery("select cus.mail from Customer cus join cus.mainOrder man WHERE man.id = 1").uniqueResult();
//
System.out.println(maxOrderNoDay);
}catch(HibernateException e){
e.printStackTrace();
}finally{
session.close();
return maxOrderNoDay;
}
}
要打印电子邮件地址,您也可以使用条件查询,有关详细参考,请访问criteria-query-example
建议:请使用正确的命名约定。
答案 1 :(得分:0)
映射文件中有错误
org.hibernate.DuplicateMappingException: Duplicate class/entity mapping com.mdr.model.EmployeeTest
检查你的地图文件,检查HEAD
request(可能这可能有帮助)
答案 2 :(得分:0)
如果将joincolumn批注添加到MainOrder类
,该怎么办?@ManyToOne
@JoinColumn(name = "customer_id", referencedColumnName = "id")
private Customer customer;
并删除
@Column(name="customer_id")
private long customerId;