gpt4 book ai didi

java - Hibernate 错误 SQLGrammarException (MySQL)

转载 作者:行者123 更新时间:2023-11-29 21:06:25 27 4
gpt4 key购买 nike

我尝试取回实例 Criteria 中的元素列表。在执行中我得到了这个异常。有什么问题吗?

我的数据库名称是“TransPlusBD”。我的 table 的名字是“gerant”。但错误表明他(它)没有找到表“TransPlusDB.gerant_gerant”,但该表不存在。通常我们必须有他的“TransPlusDB.gerant”。

配置代码

    properties = new Properties();
properties.put("hibernate.dialect","org.hibernate.dialect.MySQLInnoDBDialect");
properties.put("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
properties.put("hibernate.connection.url","jdbc:mysql://(cloud amazone aws).amazonaws.com:3306/TransPlusDB");
properties.put("hibernate.connection.username","xxx");
properties.put("hibernate.connection.password","xxxxxxxxxxxx");
properties.put("hibernate.connection.pool_size","4");
configuration = new Configuration();
configuration.setProperties(properties);

configuration.addAnnotatedClass(Administrator.class);
configuration.addAnnotatedClass(AutoGare.class);
configuration.addAnnotatedClass(Car.class);
configuration.addAnnotatedClass(City.class);
configuration.addAnnotatedClass(Company.class);
configuration.addAnnotatedClass(transplus.models.Configuration.class);
configuration.addAnnotatedClass(DateDeparture.class);
configuration.addAnnotatedClass(Departure.class);
configuration.addAnnotatedClass(HoursDeparture.class);
configuration.addAnnotatedClass(Luggage.class);
configuration.addAnnotatedClass(Manager.class);
configuration.addAnnotatedClass(Passenger.class);
configuration.addAnnotatedClass(PlanningVoyage.class);
configuration.addAnnotatedClass(Route.class);
configuration.addAnnotatedClass(Stopover.class);
configuration.addAnnotatedClass(SysAdmin.class);
configuration.addAnnotatedClass(Ticket.class);
configuration.addAnnotatedClass(TypeCar.class);

configuration.addAnnotatedClass(ModificationLuggage.class);
configuration.addAnnotatedClass(ModificationTicket.class);
configuration.addAnnotatedClass(PassageRoute.class);

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();

我的类(class)代码

@Entity
@Table(name = "gerant")
public class Manager implements Serializable // Table Gerant
{
@Id
@GeneratedValue
@Column(name = "code_gerant")
private long code_manager;

@Column(name = "matricule_gérant",unique = true)
private String matricule_manager;
@Column(name = "nom_gerant")
private String lastName_manager;
@Column(name = "prenom_gerant")
private String firstName_manager;
@Column(name = "password_gerant",nullable = false)
private String password_manager;
@Column(name = "login_gerant",unique = true,nullable = false)
private String login_manager;
@Column(name = "poste_gerant")
private String function_manager;
@Column(name = "actif_gerant")
private boolean enabled_manager;

@Enumerated(EnumType.ORDINAL)
@Column(name = "privilege_gerant")
private Privilege privilege;

@ManyToOne
@JoinColumn(name = "code_manager", foreignKey = @ForeignKey(name = "fk_gerant_manager"))
private Administrator administrator;

@ManyToOne
@JoinColumn(name = "over_gerant", foreignKey = @ForeignKey(name = "fk_over_gerant"))
private Manager overManager;

@Expose // Annotation for Gson
@OneToMany(cascade = CascadeType.ALL,orphanRemoval = false)
private List<Manager> underManagers = new ArrayList<>();

恢复列表的代码。

public String getAllManager()
{

if(session.isOpen())
{
Transaction transaction = null;
try
{
transaction = session.beginTransaction();
transaction.begin();
Criteria criteria = session.createCriteria(Manager.class);
List list = criteria.list();
transaction.commit();
if(list != null)
{
if(!list.isEmpty())
return serializeTab(list);
}
return null;
}
catch (Exception e)
{
if(transaction != null)
transaction.rollback();
e.printStackTrace();
}
}
return null;
}

这是引发的异常

    this = {ServiceManager@3681} 
transaction = {TransactionImpl@3683}
transactionCoordinator = {JdbcResourceLocalTransactionCoordinatorImpl@3979}
transactionDriverControl = {JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl@3980}
valid = false
e = {SQLGrammarException@3954} "org.hibernate.exception.SQLGrammarException: could not extract ResultSet"
sqlException = {MySQLSyntaxErrorException@3958} "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'TransPlusDB.gerant_gerant' doesn't exist"
SQLState = "42S02"
vendorCode = 1146
next = null
detailMessage = "Table 'TransPlusDB.gerant_gerant' doesn't exist"
cause = {MySQLSyntaxErrorException@3958} "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'TransPlusDB.gerant_gerant' doesn't exist"
stackTrace = {StackTraceElement[0]@3961}
suppressedExceptions = {Collections$UnmodifiableRandomAccessList@3962} size = 0
sql = "n/a"

请帮忙

最佳答案

gerant_gerant是一个用于自关联的连接表

@OneToMany(cascade = CascadeType.ALL,orphanRemoval = false)
private List<Manager> underManagers = new ArrayList<>();

您需要让 Hibernate 使用 hibernate.hbm2ddl.auto 属性创建此表,或者您可以使用约束(外键、唯一键)手动创建它。

您可以使用@JoinTable注释指定连接表名称。

关于java - Hibernate 错误 SQLGrammarException (MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36790106/

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