我正在开发 hibernate 的ManyToMany
关系示例,并面临以下错误:我不确定这里出了什么问题。欢迎任何快速帮助。
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'Group (name) values ('Administrator Group')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_45]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_45]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_45]
at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_45]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407) ~[mysql-connector-java-5.1.15.jar:na]
at com.mysql.jdbc.Util.getInstance(Util.java:382) ~[mysql-connector-java-5.1.15.jar:na]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052) ~[mysql-connector-java-5.1.15.jar:na]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603) ~[mysql-connector-java-5.1.15.jar:na]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535) ~[mysql-connector-java-5.1.15.jar:na]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1989) ~[mysql-connector-java-5.1.15.jar:na]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150) ~[mysql-connector-java-5.1.15.jar:na]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626) ~[mysql-connector-java-5.1.15.jar:na]
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119) ~[mysql-connector-java-5.1.15.jar:na]
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415) ~[mysql-connector-java-5.1.15.jar:na]
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2333) ~[mysql-connector-java-5.1.15.jar:na]
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2318) ~[mysql-connector-java-5.1.15.jar:na]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204) ~[hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57) ~[hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42) ~[hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2792) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3363) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:597) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:232) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:213) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:256) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at com.sdnext.hibernate.tutorial.main.UserGroupMain.main(UserGroupMain.java:29) [classes/:na]
22:49:51.346 [main] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1064, SQLState: 42000
Group.java
@Entity
@Table(name="Group")
public class Group {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="GROUP_ID")
private long id;
private String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "USERS_GROUPS",
joinColumns = @JoinColumn(name = "GROUP_ID"),
inverseJoinColumns = @JoinColumn(name = "USER_ID")
)
private Set<User> users = new HashSet<User>();
public Group(String name) {
super();
this.name = name;
}
// setters and getters
}
User.java
@Entity
@Table(name = "USERS")
public class User {
@Id
@GeneratedValue
@Column(name="USER_ID")
private long id;
private String username;
private String password;
private String email;
@ManyToMany(mappedBy = "users")
private Set<Group> groups = new HashSet<Group>();
public User(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
public void addGroup(Group group) {
this.groups.add(group);
}
// setters and getters
}
Main.java
public class UserGroupMain {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Group groupAdmin = new Group("Administrator Group");
Group groupGuest = new Group("Guest Group");
User user1 = new User("Tom", "tomcat", "tom@codejava.net");
User user2 = new User("Mary", "mary", "mary@codejava.net");
groupAdmin.addUser(user1);
groupAdmin.addUser(user2);
groupGuest.addUser(user1);
user1.addGroup(groupAdmin);
user2.addGroup(groupAdmin);
user1.addGroup(groupGuest);
session.save(groupAdmin);
session.save(groupGuest);
session.getTransaction().commit();
session.close();
}
}
HibernateUtil.java
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
的hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<property name="hibernate.id.new_generator_mappings">false</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="com.sdnext.hibernate.tutorial.dto.Group"/>
<mapping class="com.sdnext.hibernate.tutorial.dto.User"/>
</session-factory>
</hibernate-configuration>
答案 0 :(得分:4)
GROUP
是reserved
中的Mysql
个关键字,请参阅documentation:
标准SQL禁止表中的大多数保留字 作为列名或表名(例如,GROUP)[...]
答案 1 :(得分:0)
使用下面的实体代码
@Entity
@Table(name="\"Group\"")
如果您想使用关键字作为列名,请尝试以下
@Column(name="[desc]")
private String desc;
它将运行并创建表组