我有一名实体学生,我正在使用DB concat
方法计算全名。使用名字和姓氏创建全名。
但是,当我运行我的应用程序时,它会出错(如下所述)。
我正在使用hibernate和DB作为MySql:
@Entity
@Table(name = "students")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "studentId")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer studentId;
@Column(name = "fname")
private String fname;
@Column(name = "lname")
private String lname;
@Generated(value = GenerationTime.ALWAYS)
@Column(columnDefinition = "AS CONCAT(" + " COALESCE(fname, ''), "
+ " COALESCE(' ' + lname, ''), " + ")")
private String fullName;
//Getter Setter omitted
}
将学生实体保存如下:
public void addStudent(StudentBean studentBean)
{
Student student=new Student();
student.setFname(studentBean.getFname());
student.setLname(studentBean.getLname());
studentDAO.saveStudent(student);
}
我得到的错误如下所述:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'student_.fullName' in 'field list'
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
com.mysql.jdbc.Util.getInstance(Util.java:384)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2212)
org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82)
org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
com.sun.proxy.$Proxy60.executeQuery(Unknown Source)
org.hibernate.persister.entity.AbstractEntityPersister.processGeneratedProperties(AbstractEntityPersister.java:4365)
org.hibernate.persister.entity.AbstractEntityPersister.processInsertGeneratedProperties(AbstractEntityPersister.java:4338)
org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:80)
org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272)
org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:304)
org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:128)
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
答案 0 :(得分:0)
I think issues happens here
@Column(columnDefinition = "AS CONCAT(" + "
COALESCE(fname, ''), "
+ " COALESCE(' ' + lname, ''), " + ")")
private String fullName;
From exception I identified that there is no reference column with "fullName" in table student......
首先检查数据库表并确认该列的名称为“fullName”
然后将上面的代码块转换为
@Column(name="fullName",
columnDefinition = "
AS CONCAT(" + "
COALESCE(fname, ''), "
+ " COALESCE(' ' + lname, ''), " + ")")
private String fullName;
检查数据库并确认其是否完整名称,并将列名称与上述语句一样放置
我希望它能起作用