spring - Hibernate表外键插入: @OneToMany/@ManyToOne relation

标签 spring hibernate tomcat

我正在使用 hibernate 4 和 Maven:

package tn.onp.mvno.model;


@Entity
@Table(name="PERSON")
public class Person {

 private int id;
 private String name;
 private String surname;
 private String email;
 private String adresse;
 private String MobilePhone;

 /**
  * Get User Id
  *
  * @return int - User Id
  */
 @Id
 @GeneratedValue
 @Column(name="ID")
 public int getId() {
  return id;
 }

 /**
  * Set User Id
  *
  * @param int - User Id
  */
 public void setId(int id) {
  this.id = id;
 }


}

package tn.onp.mvno.model;


 @Entity
 @Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
 @Table(name="USER")
 public class User extends Person{

 private String SIMCardNumber;
 private String SIMCardValidityDate;
 private Collection<Call> calls; 
 private Collection<Credit> credits; 




 @Column(name="SIMCARDNUMBER", unique = true, nullable = false)
 public String getSIMCardNumber() {
return SIMCardNumber;
 }




 public void setSIMCardNumber(String sIMCardNumber) {
SIMCardNumber = sIMCardNumber;
 }


@Column(name="SIMCARDVALIDITYDATE", nullable = false)

 public String getSIMCardValidityDate() {
return SIMCardValidityDate;
}




 public void setSIMCardValidityDate(String sIMCardValidityDate) {
SIMCardValidityDate = sIMCardValidityDate;
   }




 @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
 public Collection<Call> getCalls() {
return calls;
 }




 public void setCalls(Collection<Call> calls) {
this.calls = calls;
 }
  }

调用.java

 @Entity
 @Table(name="CALL")
 public class Call {

private String callId;
private String telNumber;
private String term;
private String duration;
private String direction;
private User user;
/** Get the callId
 * @return the callId
 */
 @Id
 @GeneratedValue
     @Column(name="CALLID")
 public String getCallId() {
    return callId;
}
/** Set the callId
 * @param callId the callId to set
 */
public void setCallId(String callId) {
    this.callId = callId;
}
/** Get the telNumber
 * @return the telNumber
 */
@Column(name="TELNUMBER")
public String getTelNumber() {
    return telNumber;
}
/** Set the telNumber
 * @param telNumber the telNumber to set
 */
public void setTelNumber(String telNumber) {
    this.telNumber = telNumber;
}
/** Get the term
 * @return the term
 */
@Column(name="TERM")
 public String getTerm() {
    return term;
}
/** Set the term
 * @param term the term to set
 */
public void setTerm(String term) {
    this.term = term;
}
/** Get the duration
 * @return the duration
 */
@Column(name="DURATION")
public String getDuration() {
    return duration;
}
/** Set the duration
 * @param duration the duration to set
 */
public void setDuration(String duration) {
    this.duration = duration;
}
/** Get the direction
 * @return the direction
 */
@Column(name="DIRECTION")
public String getDirection() {
    return direction;
}
/** Set the direction
 * @param direction the direction to set
 */
public void setDirection(String direction) {
    this.direction = direction;
}
/** Get the user
 * @return the user
 */
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id")
public User getUser() {
    return user;
}
/** Set the user
 * @param user the user to set
 */
public void setUser(User user) {
    this.user = user;
}

当我在 tomcat 服务器上部署应用程序并运行它时,出现此错误:

INFO: HHH000227: Running hbm2ddl schema export
Hibernate: alter table CALL drop foreign key FK1F725EE04824FE
févr. 11, 2013 5:53:38 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table CALL drop foreign key FK1F725EE04824FE
févr. 11, 2013 5:53:38 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'CALL drop foreign key  
FK1F725EE04824FE' at line 1
Hibernate: drop table if exists CALL

févr. 11, 2013 5:53:38 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: drop table if exists CALL
févr. 11, 2013 5:53:38 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'CALL' at line 1
Hibernate: drop table if exists CREDIT
Hibernate: drop table if exists PERSON
Hibernate: create table CALL (CALLID varchar(255) not null auto_increment, DIRECTION 
varchar(255), DURATION varchar(255), TELNUMBER varchar(255), TERM varchar(255), id   
integer, primary key (CALLID))
févr. 11, 2013 5:53:38 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table CALL (CALLID varchar(255) not null   
auto_increment, DIRECTION varchar(255), DURATION varchar(255), TELNUMBER varchar(255),
TERM varchar(255), id integer, primary key (CALLID))
févr. 11, 2013 5:53:38 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near 'CALL (CALLID varchar(255) not 
null auto_increment, DIRECTION varchar(255), DURAT' at line 1

最佳答案

CALL 是一个 reserved word .选择另一个表名。

关于spring - Hibernate表外键插入: @OneToMany/@ManyToOne relation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14817108/

相关文章:

tomcat - 在 VisualVM 中禁用执行 GC 和 CPU 监视

javascript - 在 mootools request.JSON 中响应状态代码为 http 200 时调用 onFailure

java - 如何在 AWS EC2 自动缩放组中仅在一个 tomcat 实例中启动 Web 进程

带有 JNDI 数据源的 Spring Boot

hibernate - 连接池异常: Cannot get a connection,池错误等待空闲对象超时

java - 如何使用 Hibernate/JDBC 删除函数?

java - EntityManager 始终为 null 并且未正确注入(inject)

java - 如何将json响应设置为java对象

java - SpringApplicationConfiguration 无法解析为类型

Spring-Drools 集成 : Referenced file (kie-spring. xsd) 包含错误