Pessoal, sou novato em java e principalmente no hibernate. Estou tentando fazer uma conexão com MySql e recuperar dados de uma tabela. Estou usando o tutorial http://netbeans.org/kb/67/web/hibernate-webapp_pt_BR.html do próprio site do netbeans. Consigo afzer tudo até na hora de executar uma consulta pelo Editor de consultas HQL.
Segue o código das classes:
@Entity
@Table(name="configuration"
,catalog="ipo"
)
public class Configuration implements java.io.Serializable {
private Integer id;
private Smtp smtp;
private Ftp ftp;
private Language language;
private Boolean active;
private String root;
private String log;
private Date schedule;
private byte[] smtp_1;
private String teste;
public Configuration() {
}
public Configuration(Date schedule) {
this.schedule = schedule;
}
public Configuration(Smtp smtp, Ftp ftp, Language language, Boolean active, String root, String log, Date schedule, byte[] smtp_1, String teste) {
this.smtp = smtp;
this.ftp = ftp;
this.language = language;
this.active = active;
this.root = root;
this.log = log;
this.schedule = schedule;
this.smtp_1 = smtp_1;
this.teste = teste;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="Id", unique=true, nullable=false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="IdSmtp")
public Smtp getSmtp() {
return this.smtp;
}
public void setSmtp(Smtp smtp) {
this.smtp = smtp;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="IdFtp")
public Ftp getFtp() {
return this.ftp;
}
public void setFtp(Ftp ftp) {
this.ftp = ftp;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="IdLanguage")
public Language getLanguage() {
return this.language;
}
public void setLanguage(Language language) {
this.language = language;
}
@Column(name="Active")
public Boolean getActive() {
return this.active;
}
public void setActive(Boolean active) {
this.active = active;
}
@Column(name="Root", length=50)
public String getRoot() {
return this.root;
}
public void setRoot(String root) {
this.root = root;
}
@Column(name="Log", length=50)
public String getLog() {
return this.log;
}
public void setLog(String log) {
this.log = log;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name="Schedule", nullable=false, length=19)
public Date getSchedule() {
return this.schedule;
}
public void setSchedule(Date schedule) {
this.schedule = schedule;
}
@Column(name="smtp")
public byte[] getSmtp_1() {
return this.smtp_1;
}
public void setSmtp_1(byte[] smtp_1) {
this.smtp_1 = smtp_1;
}
@Column(name="Teste", length=200)
public String getTeste() {
return this.teste;
}
public void setTeste(String teste) {
this.teste = teste;
}
}
@Entity
@Table(name="ftp"
,catalog="ipo"
)
public class Ftp implements java.io.Serializable {
private Integer id;
private String login;
private String password;
private String server;
private String port;
private String senha;
private Set<Configuration> configurations = new HashSet<Configuration>(0);
public Ftp() {
}
public Ftp(String login, String password, String server, String port, String senha, Set<Configuration> configurations) {
this.login = login;
this.password = password;
this.server = server;
this.port = port;
this.senha = senha;
this.configurations = configurations;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="Id", unique=true, nullable=false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="Login", length=50)
public String getLogin() {
return this.login;
}
public void setLogin(String login) {
this.login = login;
}
@Column(name="Password", length=50)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name="Server", length=50)
public String getServer() {
return this.server;
}
public void setServer(String server) {
this.server = server;
}
@Column(name="Port", length=10)
public String getPort() {
return this.port;
}
public void setPort(String port) {
this.port = port;
}
@Column(name="senha")
public String getSenha() {
return this.senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="ftp")
public Set<Configuration> getConfigurations() {
return this.configurations;
}
public void setConfigurations(Set<Configuration> configurations) {
this.configurations = configurations;
}
}
As classes Smtp e Languagem seguem o mesmo padrão da classe FTP.
Segue o hibernate.cfg.xmtml
<?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://ipodatabase:3306/IPO</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">P@ssw0rd</property>
<property name="hibernate.default_schema">IPO</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="model.Configuration"/>
<mapping class="model.Language"/>
<mapping class="model.Smtp"/>
<mapping class="model.Ftp"/>
</session-factory>
</hibernate-configuration>
E aqui outra dúvida. No tutorial não é usado o arquivo persistence.xml, mas me recomendaram usar, então segue o código dele também:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="ModelPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="P@ssw0rd"/>
<property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/Ipo"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Segue o erro quando tento executar a consulta hql: from Ftp
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
Caused by: 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 ‘.ftp ftp0_ limit 100’ at line 1
at sun.reflect.GeneratedConstructorAccessor282.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2275)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
at org.hibernate.loader.Loader.doQuery(Loader.java:674)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
… 9 more