I have been trying now for two days to get hibernate working for a simple example
and it doesn't work yet!
It used to work before but I don't know what is happened now!
First of all , everything runs ok, and I get no error outputs when I start the application,
also the last output line says as below:
INFO: no JNDI name configured
Hibernate: insert into cd (title, artist, purchasedate, cost, ID) values (?,?,?,?,?)
But when I check my CD table on my dbms MySql4.1 I see no values, it is basically an empty table.
However the hibernate_unique_key table that I also have adds another value for every time I test my
application.
Also the last column shown in the output above is ID, in my table that is the first column.
I have tried both ways and it still don't work!
I am using MySql4.1 no transactions, just normal tables!
I have set the test folder of my java files in the class path as well.
C:\test;
Also the hibernate folder:
C:\hibernate-2\hibernate-2.1;
I have place the jdbc driver among the other hibernate files and set the classpath this way too!:
C:\hibernate-2\hibernate-2.1\lib\mysql-connector-java-3.0.16-ga-bin.jar;
below are all and the exact settings of my windows xp, in fact I have recovered it today, so it is
clean and only for hibernate.
Also the sql tables, hibernate files and and java files are explained below.
I might have missed out something here, maybe another important jar file!
Code:
ANT_HOME C:\Ant
JAVA_HOME C:\Program Files\Java\jdk1.5.0
PATH .;C:\Program Files\Java\jdk1.5.0\bin;C:\Ant\bin
CLASSPATH
.;
C:\Program Files\Java\jdk1.5.0\lib\tools.jar;
C:\hibernate-2\hibernate-2.1;
C:\test;
C:\hibernate-2\hibernate-2.1\lib\mysql-connector-java-3.0.16-ga-bin.jar;
C:\hibernate-2\hibernate-2.1\lib\cglib2.jar;
C:\hibernate-2\hibernate-2.1\lib\commons-logging.jar;
C:\hibernate-2\hibernate-2.1\lib\commons-collections.jar;
C:\hibernate-2\hibernate-2.1\lib\dom4j.jar;
C:\hibernate-2\hibernate-2.1\lib\ehcache.jar;
C:\hibernate-2\hibernate-2.1\lib\jdbc2_0-stdext.jar;
C:\hibernate-2\hibernate-2.1\lib\jta.jar;
C:\hibernate-2\hibernate-2.1\lib\odmg.jar;
C:\hibernate-2\hibernate-2.1\lib\xalan.jar;
C:\hibernate-2\hibernate-2.1\lib\xerces.jar;
C:\hibernate-2\hibernate-2.1\lib\xml-apis.jar;
C:\hibernate-2\hibernate-2.1\hibernate2.jar;
SQL Code
Code:
create database products;
use products;
create table CD (
ID int not null primary key,
title text,
artist text,
purchasedate datetime,
cost double,
);
I have tried this way too, where the primary key is the last column.
Of course I had to recreate the database and tables again!
create table CD (
title text,
artist text,
purchasedate datetime,
cost double,
ID int not null primary key
);
create table hibernate_unique_key (
next_hi int
);
insert into hibernate_unique_key values (1);
My CD class
Code:
import java.io.*;
import java.util.*;
public class CD {
int id;
String title;
String artist;
Date purchaseDate;
double cost;
public CD() {
}
public CD(String title, String artist, Date purchaseDate, double cost) {
this.title = title;
this.artist = artist;
this.purchaseDate = purchaseDate;
this.cost = cost;
}
public void setId(int id) {
this.id = id;
}
public int getId(){
return id;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getArtist() {
return artist;
}
public void setPurchasedate(Date purchaseDate) {
this.purchaseDate = purchaseDate;
}
public Date getPurchasedate() {
return purchaseDate;
}
public void setCost(double cost) {
this.cost = cost;
}
public double getCost() {
return cost;
}
}
My test class
Code:
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.event.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class CDTest extends JFrame implements ListDataListener {
JList list;
CDList listModel;
JTextField artistField;
JTextField titleField;
JTextField costField;
JTextField IDField;
JLabel IDLabel;
int selectedListIndex;
SessionFactory sessionFactory;
public CDTest(){
try {
Configuration cfg = new Configuration().addClass(CD.class);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
buildGUI();
}
private void buildGUI() {
Container container = getContentPane();
listModel = new CDList();
// listModel.addCD("Grace Under Pressure", "Rush", new Date(), 9.99);
list = new JList();
list.setModel(listModel);
list.setCellRenderer(new CDRenderer());
container.add(list, BorderLayout.NORTH);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent ae) {
JList list = (JList)ae.getSource () ;
CDList model = (CDList)list.getModel();
selectedListIndex = ((JList)ae.getSource()).getSelectedIndex();
CD cd = (CD)model.getElementAt(selectedListIndex);
IDLabel.setText(""+cd.getId());
titleField.setText(cd.getTitle());
artistField.setText(cd.getArtist());
costField.setText(""+cd.getCost());
}
});
JPanel panel = new JPanel(new GridLayout(7,2));
artistField = new JTextField(25);
titleField = new JTextField(25);
costField = new JTextField(25);
IDLabel = new JLabel();
panel.add(new JLabel("ID"));
panel.add(IDLabel);
panel.add(new JLabel("Title"));
panel.add (titleField);
panel.add(new JLabel("Artist"));
panel.add (artistField);
panel.add(new JLabel("Cost"));
panel.add(costField);
JButton button = new JButton("Update");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
CD cd = (CD)listModel.getElementAt(selectedListIndex);
cd.setTitle(titleField.getText());
cd.setArtist(artistField.getText());
cd.setCost((double)Double.parseDouble(costField.getText()));
try {
Session session = sessionFactory.openSession();
session.update(cd);
session.flush();
session.close();
} catch (Exception e) {}
IDLabel.setText("");
titleField.setText("");
artistField.setText("");
costField.setText("");
}
});
panel.add (button);
button = new JButton("Add");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
CD cd = new CD(artistField.getText(),
titleField.getText(),
new Date(),
Double.parseDouble(costField.getText()));
listModel.addCD(cd);
try {
Session session = sessionFactory.openSession();
session.save(cd);
session.flush();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
IDLabel.setText("");
titleField.setText("");
artistField.setText("");
costField.setText("");
}
});
panel.add (button);
button = new JButton("Delete");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
CD cd = (CD)listModel.getElementAt(selectedListIndex);
try {
Session session = sessionFactory.openSession();
session.delete(cd);
session.flush();
session.close();
} catch (Exception e) {}
listModel.removeElement(selectedListIndex);
}
});
panel.add (button);
button = new JButton("Pull All");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
Session session = sessionFactory.openSession();
java.util.List cds = session.find("from CD");
session.close();
listModel.addCDs(cds);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "No CD", "problem pulling cds", 0);
}
}
});
panel.add(button);
IDField = new JTextField(25);
panel.add(IDField);
button = new JButton("Pull Single");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int index = Integer.parseInt(IDField.getText());
try {
Session session = sessionFactory.openSession();
CD cd = new CD();
session.load(cd, new Integer(index));
session.close();
listModel.addCD(cd);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "No CD", "No CD with that ID", 0);
}
}
});
panel.add (button);
container.add(panel, BorderLayout.SOUTH);
setSize(300,250);
setVisible(true);
}
public void intervalAdded(ListDataEvent e) {
list.invalidate();
}
public void contentsChanged(ListDataEvent e) {
list.invalidate();
}
public void intervalRemoved(ListDataEvent e) {
list.invalidate();
}
public static void main(String [] args) {
CDTest t = new CDTest() ;
}
private class CDList extends AbstractListModel {
Vector v = new Vector();
public void addCD(String title, String artist, Date pdate, double cost) {
CD cd = new CD(title, artist, pdate, cost);
v.add(cd);
fireContentsChanged(this, 0, 0);
}
public void addCD(CD cd) {
v.add(cd) ;
fireContentsChanged(this, 0, 0);
}
public void addCDs(java.util.List cds) {
v.addAll(cds);
fireContentsChanged(this, 0, 0);
}
public int getSize() {
return v.size();
}
public void removeElement(int index) {
v.removeElementAt(index);
fireContentsChanged(this, 0, 0);
}
public Object getElementAt(int index) {
return v.elementAt(index);
}
}
private class CDRenderer extends JLabel implements ListCellRenderer {
private Color HIGHLIGHT = new Color(0,0,128);
public CDRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
CD cd = (CD)value; setText(cd.getTitle());
if (isSelected) {
setBackground(HIGHLIGHT);
setForeground(Color.white);
} else {
setBackground(Color.white);
setForeground(Color.black);
}
return this;
}
}
}
CD.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="CD"
table="cd">
<id name="id"
type="int"
unsaved-value="null">
<column name="ID"
sql-type="int"
not-null="true"/>
<generator class="hilo"/>
</id>
<property name="title"/>
<property name="artist"/>
<property name="purchasedate" type="date"/>
<property name="cost" type="double"/>
</class>
</hibernate-mapping>
I have tried with and without this (hibernate.cfg.xml2).
hibernate.cfg.xml2
Code:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-configuration>
<session-factory name="java:comp/env/hibernate/SessionFactory">
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/products</property>
<property name="connection.username">root</property>
<property name="connection.password">secret</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/products</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<mapping resource="CD.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate.properties
Code:
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost/products
hibernate.connection.username root
hibernate.connection.password secret
hibernate.show_sql true
Code:
What can be missing????????????????????????????????????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Thank you!