我目前正在使用NetBeans 8.0.2 Java项目,我有这个代码:
分支地址:
package de.hft.carrental.domain;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "branch_address")
public class BranchAddress {
private String cityName;
private String country;
private Integer id;
private String phoneNumber;
private String postalCode;
private String streetName;
private String streetNumber;
private Branch branch;
@Column(name = "city_name", updatable = false, nullable = false, length =
45)
public String getCityName() {
return cityName;
}
@Column(name = "country", updatable = false, nullable = false, length =
45)
public String getCountry() {
return country;
}
@Id
@GeneratedValue
@Column(name = "branch_id", updatable = false, nullable = false)
public Integer getId() {
return id;
}
@Column(name = "phone_number", updatable = true, nullable = false, length
= 45)
public String getPhoneNumber() {
return phoneNumber;
}
@Column(name = "postal_code", updatable = true, nullable = false, length =
10)
public String getPostalCode() {
return postalCode;
}
@Column(name = "street_name", updatable = true, nullable = false, length =
45)
public String getStreetName() {
return streetName;
}
@Column(name = "street_number", updatable = true, nullable = false, length
= 5)
public String getStreetNumber() {
return streetNumber;
}
@OneToOne(cascade = CascadeType.ALL, optional = false, targetEntity =
Branch.class)
@JoinColumn(name = "branch_id", unique = true, updatable = false, nullable
= false)
public Branch getBranch() {
return branch;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public void setCountry(String country) {
this.country = country;
}
public void setId(Integer id) {
this.id = id;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public void setStreetNumber(String streetNumber) {
this.streetNumber = streetNumber;
}
public void setBranch(Branch branch) {
this.branch = branch;
}
}

分支:
package de.hft.carrental.domain;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "branch")
public class Branch {
private Integer id;
private String name;
private Agency agency;
private BranchAddress branchAddress;
@Id
@GeneratedValue
@Column(name = "id", updatable = false, nullable = false)
public Integer getId() {
return id;
}
@Column(name = "branch_name", updatable = true, nullable = false, length = 45)
public String getName() {
return name;
}
@ManyToOne(optional = false, targetEntity = Agency.class)
@JoinColumn(name = "agency_id", updatable = false, nullable = false,
referencedColumnName = "id")
public Agency getAgency() {
return agency;
}
@OneToOne(cascade = CascadeType.ALL, mappedBy = "branch", optional =
false, orphanRemoval = true, targetEntity = BranchAddress.class)
public BranchAddress getBranchAddress() {
return branchAddress;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAgency(Agency agency) {
this.agency = agency;
}
public void setBranchAddress(BranchAddress address) {
branchAddress = address;
}
}

客户地址
package de.hft.carrental.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "customer_address")
public class CustomerAddress {
private String cityName;
private String country;
private Integer id;
private String phoneNumber;
private String postalCode;
private String streetNumber;
private String streetName;
private Customer customer;
@Column(name = "city_name", updatable = true, nullable = false, length =
45)
public String getCityName() {
return cityName;
}
@Column(name = "country", updatable = true, nullable = false, length = 45)
public String getCountry() {
return country;
}
@Id
@GeneratedValue
@Column(name = "id", updatable = false, nullable = false)
public Integer getId() {
return id;
}
@Column(name = "phone_number", updatable = true, nullable = false, length
= 45)
public String getPhoneNumber() {
return phoneNumber;
}
@Column(name = "postal_code", updatable = true, nullable = false, length =
10)
public String getPostalCode() {
return postalCode;
}
@Column(name = "street_name", updatable = true, nullable = false, length =
45)
public String getStreetName() {
return streetName;
}
@Column(name = "street_number", updatable = true, nullable = false, length
= 5)
public String getStreetNumber() {
return streetNumber;
}
@ManyToOne(optional = false, targetEntity = Customer.class)
@JoinColumn(name = "customer_id", updatable = false, nullable = false,
referencedColumnName = "id")
public Customer getCustomer() {
return customer;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public void setCountry(String country) {
this.country = country;
}
public void setId(Integer id) {
this.id = id;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public void setStreetNumber(String streetNumber) {
this.streetNumber = streetNumber;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}

客户:
package de.hft.carrental.domain;
import java.util.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "customer")
public class Customer {
public static final String CUSTOMER_TYPE_PRIVATE = "private";
public static final String CUSTOMER_TYPE_COMPANY = "company";
private String companyName;
private String customerType;
private Date dateOfBirth;
private String email;
private String firstName;
private Integer id;
private String loginName;
private String password;
private Date registerDate;
private String surname;
private Set<BranchAddress> customerAddresses;
private Set<Booking> bookings;
@Column(name = "company_name", updatable = true, nullable = true)
public String getCompanyName() {
return companyName;
}
@Column(name = "customer_type", updatable = true, nullable = false)
public String getCustomerType() {
return customerType;
}
@Column(name = "date_of_birth", updatable = true, nullable = true)
public Date getDateOfBirth() {
return dateOfBirth;
}
@Column(name = "email", updatable = true, nullable = false, length = 45)
public String getEmail() {
return email;
}
@Column(name = "first_name", updatable = true, nullable = true, length =
45)
public String getFirstName() {
return firstName;
}
@Id
@GeneratedValue
@Column(name = "id", updatable = false, nullable = false)
public Integer getId() {
return id;
}
@Column(name = "login_name", updatable = false, nullable = false, length =
45)
public String getLoginName() {
return loginName;
}
@Column(name = "password", updatable = true, nullable = false, length =
45)
public String getPassword() {
return password;
}
@Column(name = "register_date", updatable = false, nullable = false)
public Date getRegisterDate() {
return registerDate;
}
@Column(name = "surname", updatable = true, nullable = true, length = 45)
public String getSurname() {
return surname;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer", orphanRemoval
= true, targetEntity = BranchAddress.class)
public Set<BranchAddress> getCustomerAddresses() {
return customerAddresses;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer", orphanRemoval
= true, targetEntity = Booking.class)
public Set<Booking> getBookings() {
return bookings;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public void setCustomerType(String customerType) {
this.customerType = customerType;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public void setEmail(String email) {
this.email = email;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setId(Integer id) {
this.id = id;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public void setPassword(String password) {
this.password = password;
}
public void setRegisterDate(Date registerDate) {
this.registerDate = registerDate;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setBookings(Set<Booking> bookings) {
if (this.bookings == null) {
this.bookings = bookings;
} else {
this.bookings.clear();
this.bookings.addAll(bookings);
}
}
public void setCustomerAddresses(Set<BranchAddress> customerAddresses) {
if (this.customerAddresses == null) {
this.customerAddresses = customerAddresses;
} else {
this.customerAddresses.clear();
this.customerAddresses.addAll(customerAddresses);
}
}
}
&#13;
当我尝试执行程序时,我从hibernate获取此错误:
Dec 28, 2014 9:52:57 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1054, SQLState: 42S22
Dec 28, 2014 9:52:57 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unknown column 'branchaddr0_.branch_id' in 'field list'
Dec 28, 2014 9:52:57 PM org.hibernate.event.internal.DefaultLoadEventListener onLoad
INFO: HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Exception in thread "AWT-EventQueue-0" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:449)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:202)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4120)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:502)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:467)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:212)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:274)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:150)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1066)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:985)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:673)
at org.hibernate.type.EntityType.resolve(EntityType.java:489)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.performTwoPhaseLoad(AbstractRowReader.java:244)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:215)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:140)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:100)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:92)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1893)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:555)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:260)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:551)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:140)
at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:180)
at de.hft.carrental.ui.main.bookings.CurrentBookingsTableSection.fillTableWithData(CurrentBookingsTableSection.java:103)
at de.hft.carrental.ui.main.bookings.CurrentBookingsTableSection.refresh(CurrentBookingsTableSection.java:54)
at de.hft.carrental.ui.WindowPage.refresh(WindowPage.java:68)
at de.hft.carrental.ui.Window.switchPageTo(Window.java:70)
at de.hft.carrental.ui.main.MainWindow.showCurrentBookingsPage(MainWindow.java:86)
at de.hft.carrental.ui.main.MainWindow.<init>(MainWindow.java:51)
at de.hft.carrental.ui.splash.login.LoginSection.performLogin(LoginSection.java:98)
at de.hft.carrental.ui.splash.login.LoginSection.access$000(LoginSection.java:27)
at de.hft.carrental.ui.splash.login.LoginSection$1.keyReleased(LoginSection.java:54)
at java.awt.Component.processKeyEvent(Component.java:6486)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2829)
at java.awt.Component.processEvent(Component.java:6302)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
at java.awt.Component.dispatchEventImpl(Component.java:4752)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'branchaddr0_.branch_id' in 'field list'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
... 74 more
&#13;
我一次又一次地看着我的映射是正确的我有1个hibernate.xml用于映射但是我得到那个错误可以有人帮助我吗?
答案 0 :(得分:0)
问题在于您的代码中存在错误:
private Set<BranchAddress> customerAddresses;
必须是:
private Set<CustomerAddress> customerAddresses;