Я искал this tutorial, чтобы узнать, используя спящий режим. Все работы с sakila db.Hibernate first hql query ... error
Теперь я пытаюсь применить эти знания к моей БД, но когда я пытаюсь запустить HQL Query (просто «от пользователя»), я получаю сообщение об ошибке:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2065)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
at org.hibernate.loader.Loader.doQuery(Loader.java:909)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2551)
at org.hibernate.loader.Loader.doList(Loader.java:2537)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2367)
at org.hibernate.loader.Loader.list(Loader.java:2362)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:939)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:229)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1260)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from limit 100' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1962)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
... 15 more
это то же самое если я написал строчку в учебнике («из фильма» вместо «из фильма»).
User.java
package model.pojos;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* User generated by hbm2java
*/
@Entity
@Table(name="user"
,catalog="mywheel"
)
public class User implements java.io.Serializable {
private Integer oid;
private UserType userType;
private String username;
private String password;
private String email;
private String address;
private Set<Comments> commentses = new HashSet<Comments>(0);
private Set<Orthopedy> orthopedies = new HashSet<Orthopedy>(0);
private Set<Multimedia> multimedias = new HashSet<Multimedia>(0);
private Set<Wheelchair> wheelchairs = new HashSet<Wheelchair>(0);
private Set<Wheelchair> wheelchairs_1 = new HashSet<Wheelchair>(0);
public User() {
}
public User(UserType userType, String username, String password, String email) {
this.userType = userType;
this.username = username;
this.password = password;
this.email = email;
}
public User(UserType userType, String username, String password, String email, String address, Set<Comments> commentses, Set<Orthopedy> orthopedies, Set<Multimedia> multimedias, Set<Wheelchair> wheelchairs, Set<Wheelchair> wheelchairs_1) {
this.userType = userType;
this.username = username;
this.password = password;
this.email = email;
this.address = address;
this.commentses = commentses;
this.orthopedies = orthopedies;
this.multimedias = multimedias;
this.wheelchairs = wheelchairs;
this.wheelchairs_1 = wheelchairs_1;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="oid", unique=true, nullable=false)
public Integer getOid() {
return this.oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="user_type_oid", nullable=false)
public UserType getUserType() {
return this.userType;
}
public void setUserType(UserType userType) {
this.userType = userType;
}
@Column(name="username", nullable=false, length=15)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name="password", nullable=false, length=15)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name="email", nullable=false, length=30)
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name="address", length=50)
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="user")
public Set<Comments> getCommentses() {
return this.commentses;
}
public void setCommentses(Set<Comments> commentses) {
this.commentses = commentses;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="user")
public Set<Orthopedy> getOrthopedies() {
return this.orthopedies;
}
public void setOrthopedies(Set<Orthopedy> orthopedies) {
this.orthopedies = orthopedies;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="user")
public Set<Multimedia> getMultimedias() {
return this.multimedias;
}
public void setMultimedias(Set<Multimedia> multimedias) {
this.multimedias = multimedias;
}
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="favorite", catalog="mywheel", joinColumns = {
@JoinColumn(name="user_oid", nullable=false, updatable=false) }, inverseJoinColumns = {
@JoinColumn(name="wheelchair_oid", nullable=false, updatable=false) })
public Set<Wheelchair> getWheelchairs() {
return this.wheelchairs;
}
public void setWheelchairs(Set<Wheelchair> wheelchairs) {
this.wheelchairs = wheelchairs;
}
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="own", catalog="mywheel", joinColumns = {
@JoinColumn(name="user_oid", nullable=false, updatable=false) }, inverseJoinColumns = {
@JoinColumn(name="wheelchair_oid", nullable=false, updatable=false) })
public Set<Wheelchair> getWheelchairs_1() {
return this.wheelchairs_1;
}
public void setWheelchairs_1(Set<Wheelchair> wheelchairs_1) {
this.wheelchairs_1 = wheelchairs_1;
}
}
hibernate.config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mywheel?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.connection.autocommit">true</property>
<mapping class="model.pojos.User"/>
<mapping class="model.pojos.Multimedia"/>
<mapping class="model.pojos.Brand"/>
<mapping class="model.pojos.Permissions"/>
<mapping class="model.pojos.Feature"/>
<mapping class="model.pojos.Comments"/>
<mapping class="model.pojos.FeatureType"/>
<mapping class="model.pojos.MultimediaType"/>
<mapping class="model.pojos.Wheelchair"/>
<mapping class="model.pojos.Material"/>
<mapping class="model.pojos.Orthopedy"/>
<mapping class="model.pojos.UserType"/>
<mapping class="model.pojos.FrameType"/>
</session-factory>
</hibernate-configuration>
SQL таблица
CREATE TABLE IF NOT EXISTS `mywheel`.`user` (
`oid` INT(11) NOT NULL AUTO_INCREMENT,
`username` CHAR(15) NOT NULL,
`password` CHAR(15) NOT NULL,
`email` CHAR(30) NOT NULL,
`address` VARCHAR(50) NULL,
`user_type_oid` INT NOT NULL,
PRIMARY KEY (`oid`),
INDEX `fk_user_user_type1_idx` (`user_type_oid` ASC),
CONSTRAINT `fk_user_user_type1`
FOREIGN KEY (`user_type_oid`)
REFERENCES `mywheel`.`user_type` (`oid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 3
DEFAULT CHARACTER SET = latin1;
Я использую Anno но у меня есть такая же проблема с отображением xml.
UPDATE Небрежно я изменил путь моего проекта в другой раздел ... так что теперь у меня есть C: ... \ приложение и D: ... \ приложение.
в NetBeans первый проект (в C :) дайте мне ошибку я уже писал ... вторую (D :) возвращает другую ошибку:
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: model.pojos.Wheelchair.materials[model.pojos.Material]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1134)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:793)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
или ... XML отображение без аннотации
org.hibernate.MappingException: An association from the table wc_material refers to an unmapped class: model.pojos.Material
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1805)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1739)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
Возможно, это зависит от раздела, где установлен XAMPP (D :)? я не думаю, что так ... потому что Sakila дб отлично работает в C: ...
в этом случае погрешность составляет около неподключенного класса ... но в hibernate.reveng.xml я добавил все таблицы из моей БД, но не создавать все классы
hibernate.reveng.xml and generated classes
Какая версия MySQL вы версия? Похоже на диалектную проблему. Ваш конфигурационный файл hibernate говорит диалект как 'org.hibernate.dialect.MySQLDialect', и это справедливо из MySQL ver <5.x. Если вы используете более поздние версии MySQL, вам нужно изменить его на соответствующий диалект, например 'org.hibernate.dialect.MySQL5Dialect/MySQL5InnoDBDialect /' и т. Д. –
я уже пробовал ... это не проблема ... также потому, что сакила db WORKS – Marco
[HALF SOLVED] ok, чтобы решить первую проблему, я должен был очистить и построить свой проект ... – Marco