Hibernate

Hi everyone, I'm new to Hibernate and I need some help.

I have a little program that I wrote that essentially inserts a contact (fields: id, firstname, lastname, email) into a database (hibernate) on my server (localhost).

For some reason, in the mapping file, it is telling me that it can't resolve the table "contacts" (I assure you, this is the exact name of the table). Here's my mapping and config:


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="Contact" table="contacts">
   <id name="id" type="long" column="id" >
   <generator />
  </id>

  <property name="firstName">
     <column name="firstname" />
  </property>
  <property name="lastName">
    <column name="lastname"/>
  </property>
  <property name="email">
    <column name="email"/>
  </property>
</class>
</hibernate-mapping>





Config:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
      <property name="hibernate.connection.driver_class">

com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">

jdbc:mysql://localhost/hibernate</property>
      <property name="hibernate.connection.username">joe</property>
      <property name="hibernate.connection.password">*password*</property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- Mapping files -->
      <mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>






I have the drivers, the database is named "hibernate" (w/o quotes), and.... I don't know what to do!

TIA, -Joe


***************************************
THANK YOU SO MUCH FOR YOUR PATIENCE AND HELP!
I finally associated the datasource with my session factory. I didn't realize there was a dropdown for the datasource (duuuuhhh!)

15 comments
Comment actions Permalink

Add an Hibernate facet to the relevant module (if you haven't do so
already).

In the Hibernate facet configuration screen, you can associate the
hibernate facet with a specific datasource.

0
Comment actions Permalink

I've already applied the Hibernate facet.

0
Comment actions Permalink

Did you create a datasource and associate it to the Hibernate facet?

If so, does the relevant table show up in the datasource tree? Does IDEA
resolve the table properly when launching a SQL console from the datasource?

0
Comment actions Permalink

Could you please also post the exact error message and the SQL used to
create the table.


Donald

0
Comment actions Permalink

I'm using a Hibernate-provided JDBC connection, so I guess hibernate.cfg.xml (the config i posted in my original post) would be my datasource, right?

Yes, I associated the config xml file with the Hibernate facet.

I also should add that when I configure the Hibernate facet, I choose the Hibernate configuration XML file I made under "Session Factory", but there is nothing under "Data source" next to it.
If I need that "Data Source", how do I make one?

0
Comment actions Permalink

The table is all in place, it has worked before. I created it with phpMyAdmin.

     contacts
id               int(4)     AUTO_INCREMENT PRIMARY
firstname    varchar(256)
lastname    varchar(256)
email          varchar(256)


The error messages I am getting:

"Cannot resolve column lastname"
"Cannot resolve column id"
"Cannot resolve column email"
"Cannot resolve table contacts"
"Cannot resolve column firstname"

0
Comment actions Permalink

Are you successfully connecting to the database? (you could run a simple
SQL query in your code to check this)

Could you post more text from around the error messages so we can see
what the context is?

Donald

no_reply@jetbrains.com wrote:

The table is all in place, it has worked before. I created it with phpMyAdmin.

  • contacts*

id int(4) AUTO_INCREMENT PRIMARY
firstname varchar(256)
lastname varchar(256)
email varchar(256)


The error messages I am getting:

"Cannot resolve column lastname"
"Cannot resolve column id"
"Cannot resolve column email"
"Cannot resolve table contacts"
"Cannot resolve column firstname"

0
Comment actions Permalink

It doesn't matter what the actual datasource for your application is at
runtime.

In order for IntelliJ to provide proper code completion and validation
for database tables/columns, it needs to know about the database
structure of your application as well. So, I'm referring to the
IntelliJ datasource. Think of it as a "development-time datasource",
or something like that.

To create one:
Window -> Tool Windows -> Data sources
Add ("plus" icon) -> JDBC data source

As an alternative, you could try the "Import" button in the "Date
sources" tool window. This makes IntelliJ search your project for some
specific configuration files (like "hibernate.cfg.xml"), from which it
can directly import a datasource definition.

However, if that fails, you can always define a JDBC data source
manually (jdbc url, driver jar, driver class, etc).

Once you have a datasource configured, test it by opening an SQL console
on it ("console" button in datasource tool window), and type some
queries. IDEA should provide SQL code completion here, for table and
column names.

If this step works, go to the definition of the datasource, and invoke
"Refresh Tables". This makes IntelliJ retrieve the database structure.

Next, open "Project Structure" (Ctrl-Shift-Alt-S).
Select your Hibernate facet (though either "Facets" or "Modules").

The options screen for the Hibernate facet has a pane named "DataSources
Mapping". Here you can associate your Hiberante session factory with a
specific IntelliJ datasource.

After this step, SQL table/column code completion and validation should
work in .hbm files as well.

Hope that helps,
-tt

0
Comment actions Permalink

This sounds awesome!... except, unfortunately, I'm working in IDEA 7. (I feel outdated now)
If I knew how to do what you just told me to in IDEA 7 I bet the problem would be fixed.

0
Comment actions Permalink

If this helps:


    Error:Error:line (16)Cannot resolve column lastname
    Error:Error:line (8)Cannot resolve column id
    Error:Error:line (19)Cannot resolve column email
    Error:Error:line (13)Cannot resolve column firstname
    Error:Error:line (7)Cannot resolve table contacts



 


I don't know how to get the verbatim log as IntelliJ automatically makes it nicey-nice for me

0
Comment actions Permalink

OK, I found the datasources window (it was under Tools | Datasources...)

It's weird -- my table and all the fields are there. I couldn't find the SQL console but I did refresh the table and clicked "Test Connection" so I assume it's working.
However, when I go to the Hibernate facet in the project/module settings and go to "DataSources Mapping," the only item that appears in the list is my session factory (/src/hibernate.cfg.xml).

0
Comment actions Permalink

What you're after is indeed part of IDEA 7.0.

Before jumping on the IDEA 8.0 early access program, I've been using the
features you're looking for for a long time, and with great pleasure :)

Just as in 8.0, in the 7.0 version of IDEA, in the Hibernate facet,
there's a "DataSources Mapping" panel. For each configured/detected
session factory, there's a dropdown to the right side. Click it to
"pair" the newly configured datasource to a session factory.

I think that by default no datasource gets (pre)selected. Maybe the
empty appearance of that part of the dialog is what's causing the
confusion? If you don't succeed in setting things up, please attach a
screenshot of the dialog you're looking at.

-tt


0
Comment actions Permalink

Joe Schmoe wrote:

OK, I found the datasources window (it was under Tools | Datasources...)

It's weird -- my table and all the fields are there. I couldn't find the SQL console but I did refresh the table and clicked "Test Connection" so I assume it's working.
However, when I go to the Hibernate facet in the project/module settings and go to "DataSources Mapping," the only item that appears in the list is my session factory (/src/hibernate.cfg.xml).

---
Original message URL: http://www.jetbrains.net/devnet/message/5232765#5232765


As mentioned in my other reply, you should not be seeing a list, but a
table. More specifically, a two-column table.

First column consist of configured/detected Hibernate session factories
- I assume you'll only be seeing one in your case.
Second column consists of a combo box for each row. The combo box allows
to match a specific datasource to a session factory.

After selecting the proper one, you should be in business.

It's expected that you're not seeing the button for the SQL console in
the Datasource tool window. SQL support was added in IDEA 8.0. Apart
from the SQL console, IDEA 8.0 can provide code completion/validation
inside SQL strings in Java code. Specifically for JDBC API methods, and
the wrappers on top of it provided by Spring and Hibernate.

-tt

0
Comment actions Permalink

I'm sorry but no, that wasn't what I was hoping for. What is the program
doing when it prints those error messages? That looks like an SQL or
Hibernate initialization error but without having some idea of the
context, it's hard to be sure.

I had hoped that by looking at, say 20 lines above and below the error
messages that it might be possible to at least constrain the possible
causes.

Joe Schmoe wrote:

If this helps:


Error:Error:line (16)Cannot resolve column lastname
Error:Error:line (8)Cannot resolve column id
Error:Error:line (19)Cannot resolve column email
Error:Error:line (13)Cannot resolve column firstname
Error:Error:line (7)Cannot resolve table contacts



I don't know how to get the verbatim log as IntelliJ automatically makes it nicey-nice for me

0
Comment actions Permalink

Thank you for your help but my issue has been resolved. The problem was that I forgot to assign a datasource.

0

Please sign in to leave a comment.