0
package model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "USERDETAILS")
public class UserDetails {
private int userId;
private String userName;
private Date joineddate;
private String description;
private Vehicle vehicle;
@OneToOne
public Vehicle getVehicle() {
return vehicle;
}
public void setVehicle(Vehicle vehicle) {
this.vehicle = vehicle;
}
public void setJoineddate(Date joineddate) {
this.joineddate = joineddate;
}
@Temporal(TemporalType.DATE)
public Date getJoineddate() {
return joineddate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@Column(name = "User_Name")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
在這裏,我創建的UserDetails表無效的列名稱vehicle_userId「:異常線程「main」 org.hibernate.exception.SQLGrammarException:無法執行語句
package model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Vehicle {
private int vehicleId;
private String vehicleName;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="userId")
public int getVehicleId() {
return vehicleId;
}
public void setVehicleId(int vehicleId) {
this.vehicleId = vehicleId;
}
public String getVehicleName() {
return vehicleName;
}
public void setVehicleName(String vehicleName) {
this.vehicleName = vehicleName;
}
}
我在這裏創建汽車表採用一對一的映射
package hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
//import model.Address;
import model.UserDetails;
import model.Vehicle;
public class HibernateTest {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();
UserDetails user = new UserDetails();
user.setUserName("bhagyasri");
user.setDescription("bhagyasri's description");
user.setJoineddate(new Date());
Vehicle vehicle = new Vehicle();
vehicle.setVehicleName("hero");
user.setVehicle(vehicle);
session.beginTransaction();
session.save(user);
session.save(vehicle);
session.getTransaction().commit();
session.close();
}
}
我收到錯誤這樣
Jan 16, 2015 4:17:43 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 207, SQLState: S0001
Jan 16, 2015 4:17:43 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Invalid column name 'vehicle_userId'.
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
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.executeUpdate(ResultSetReturnImpl.java:211)
Hibernate配置
<?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="hbm2ddl.auto">create</property>
<property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="connection.url">jdbc:sqlserver://192.168.2.219\SQLEXPRESS;databaseName=train</property>
<property name="connection.username">train</property>
<property name="connection.password">[email protected]</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="show_sql">true</property>
<mapping class="model.UserDetails" />
<mapping class="model.Vehicle" />
</session-factory>
</hibernate-configuration>
,這是我的配置文件..我收到無效的列名稱vehicle_userId「:SQLGrammarException我沒有得到什麼確切的問題有什麼建議?
再次拋出相同的錯誤無效的列名'vehicle_vehicleId'。 線程「main」中的異常org.hibernate.exception.SQLGrammarException:無法執行語句 \t at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123) –
您確定沒有任何更多映射到'Vehicle'?你能打開SQL日誌並用它更新問題嗎? –