gpt4 book ai didi

java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : You have an error in your SQL syntax; check the manual that corresponds to your MySQL

转载 作者:行者123 更新时间:2023-11-29 03:00:29 24 4
gpt4 key购买 nike

我正在开发 hibernateManyToMany 关系示例并面临以下错误:我不确定这里出了什么问题。感谢您提供任何快速帮助。

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

组.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
}

用户.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
}

主.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();
}
}

hibernate 工具.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>

最佳答案

GROUPMysql 中的保留关键字,参见documentation :

Most of the reserved words in the table are forbidden by standard SQL as column or table names (for example, GROUP) [...]

关于java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : You have an error in your SQL syntax; check the manual that corresponds to your MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com