我有3张桌子
城市,学院和大学。
城市有许多与大学的映射
大学与学院有多对一的映射。
但是当我插入数据时,我得到了异常。
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "table"
Position: 13
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:365)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:186)
... 21 more
@Entity
@Table(name="city")
public class City {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private long cityID;
private String cityName;
@ManyToOne
@JoinColumn(name="state_id")
private State state;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "city_university",
joinColumns = @JoinColumn(name = "cityID"),
inverseJoinColumns = @JoinColumn(name = "universityID")
)
private Set<University> universities = new HashSet<University>();
/**
* @return the cityID
*/
public long getCityID() {
return cityID;
}
/**
* @param cityID the cityID to set
*/
public void setCityID(long cityID) {
this.cityID = cityID;
}
/**
* @return the cityName
*/
public String getCityName() {
return cityName;
}
/**
* @param cityName the cityName to set
*/
public void setCityName(String cityName) {
this.cityName = cityName;
}
/**
* @return the state
*/
public State getState() {
return state;
}
/**
* @param state the state to set
*/
public void setState(State state) {
this.state = state;
}
/**
* @return the universities
*/
public Set<University> getUniversities() {
return universities;
}
/**
* @param universities the universities to set
*/
public void setUniversities(Set<University> universities) {
this.universities = universities;
}
}
/**
* * / 包com.junaid.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* @author jamju02
*
*/
@Entity
@Table(name="university")
public class University {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private long universityID;
private String universityName;
@ManyToMany(mappedBy = "universities")
private Set<City> cities = new HashSet<City>();
@OneToMany(mappedBy="university")
private Set<College> colleges;
/**
* @return the universityID
*/
public long getUniversityID() {
return universityID;
}
/**
* @param universityID the universityID to set
*/
public void setUniversityID(long universityID) {
this.universityID = universityID;
}
/**
* @return the universityName
*/
public String getUniversityName() {
return universityName;
}
/**
* @param universityName the universityName to set
*/
public void setUniversityName(String universityName) {
this.universityName = universityName;
}
/**
* @return the cities
*/
public Set<City> getCities() {
return cities;
}
/**
* @param cities the cities to set
*/
public void setCities(Set<City> cities) {
this.cities = cities;
}
/**
* @return the colleges
*/
public Set<College> getColleges() {
return colleges;
}
/**
* @param colleges the colleges to set
*/
public void setColleges(Set<College> colleges) {
this.colleges = colleges;
}
}
/**
*
*/
package com.junaid.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* @author jamju02
*
*/
@Entity
@Table(name="table")
public class College {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private long collegeID;
private String collegeName;
@ManyToOne
@JoinColumn(name="university_id")
private University university;
/**
* @return the collegeID
*/
public long getCollegeID() {
return collegeID;
}
/**
* @param collegeID the collegeID to set
*/
public void setCollegeID(long collegeID) {
this.collegeID = collegeID;
}
/**
* @return the collegeName
*/
public String getCollegeName() {
return collegeName;
}
/**
* @param collegeName the collegeName to set
*/
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
/**
* @return the university
*/
public University getUniversity() {
return university;
}
/**
* @param university the university to set
*/
public void setUniversity(University university) {
this.university = university;
}
}
插入数据的主要类是
public class Test{
/* public void testApp() {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Country cou = new Country();
cou.setCountryName("India");
session.save(cou);
State state1 = new State();
State state2 = new State();
state1.setStateName("Karnataka");
state1.setCountry(cou);
state2.setStateName("Tamil Nadu");
state2.setCountry(cou);
session.save(state1);
session.save(state2);
session.getTransaction().commit();
session.close();
}*/
public static void main(String[] arg){
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Country cou = new Country();
cou.setCountryName("India");
session.save(cou);
State state1 = new State();
State state2 = new State();
state1.setStateName("Karnataka");
state1.setCountry(cou);
state2.setStateName("Tamil Nadu");
state2.setCountry(cou);
session.save(state1);
session.save(state2);
City c1 = new City();
c1.setCityName("Bangalore");
c1.setState(state1);
City c2 = new City();
c2.setCityName("Chennai");
c2.setState(state2);
University university1 = new University();
university1.setUniversityName("VTU");
university1.getCities().add(c1);
University university2 = new University();
university2.setUniversityName("ANNA");
university2.getCities().add(c2);
c1.getUniversities().add(university1);
c2.getUniversities().add(university2);
session.save(c1);
session.save(c2);
session.getTransaction().commit();
session.close();
/* SessionFactory sessionFactory1 = new Configuration().configure()
.buildSessionFactory();
Session session1 = sessionFactory1.openSession();
session1.beginTransaction();
College col1 = new College();
col1.setCollegeName("HKBK");
col1.setUniversity(university1);
College col2 = new College();
col2.setCollegeName("test");
col2.setUniversity(university2);
session1.save(col1);
session1.getTransaction().commit();
session1.close();*/
}
}
答案 0 :(得分:0)
@Table(name="table")
为College表选择另一个名称。 college
将是一个更好的选择。 table
是一个SQL关键字。