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表
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">tspl@123</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>
这是我的配置文件..我收到无效的列名&#39; vehicle_userId&#39;:SQLGrammarException我没有得到什么是确切的问题有什么建议吗?
答案 0 :(得分:0)
如果您只是将@OneToOne
放在某个映射上(在您的情况下为vehicle
),则默认的预期列名称为<field_name>_<target_id_column_name>
,在您的情况下会解析为vehicle_userId
,因为字段名称为vehicle
,@Column(name="userId")
上有Vehicle#getVehicleId()
。
首先,vehicleId
的列可能不对(因为它名为用户 ID)。所以,将其改为
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="vehicleId")
public int getVehicleId() {
return vehicleId;
}
并在UserDetails
更改为此
@OneToOne
@JoinColumn(name = "vehicle_id")
public Vehicle getVehicle() {
return vehicle;
}
如果您有列USERDETAILS.VEHICLE_ID
,这将有效。