HI i am getting this error ..tried a lott but have nt been able to find a solution. Can anyone please help me ....you can find the error from this link https://drive.google.com/file/d/0BxEeexgXf53PQ1JqZWQ4SEloSjg/view?usp=sharing
hibernate.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class catalog="user" name="beans.UserDetail" table="USERS"/>
<id column="ID" name="id" type="java.lang.Integer">
<generator class="assigned"/>
</id>
<property column="UNAME" name="uname" type="string"/>
<property column="PWORD" name="pword" type="string"/>
</hibernate-mapping>
hibernate.cfg.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.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/dbtest </property>
<property name="hibernate.connection.username">root</property>
<property name="connection.password">pass</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="current_session_context_class">thread</property>
<property name = "show_sql">true</property>
<property name = "format_sql">true</property>
<property name = "hibernate.transaction.factory_class">org.hibernate.testing.cache.CachingRegionFactory
</property>
<property name = "hibernate.hbm2ddl.auto">create</property>
<mapping resource="config/hibernate.hbm.xml"></mapping>
<mapping class = "beans/insert"></mapping>
</session-factory>
</hibernate-configuration>
userdetail.java
package beans;
import javax.persistence.Entity;
import javax.persistence.Id;
#Entity
public class UserDetail {
#Id private int id;
String uname;
String pword;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPword() {
return pword;
}
public void setPword(String pword) {
this.pword = pword;
}
}
insert.java
package beans;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class insert {
public static void main(String[] args){
SessionFactory sf = (SessionFactory) new Configuration().configure("config/hibernate.cfg.xml").buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
UserDetail user1 = new UserDetail();
user1.setId(7);
user1.setUname("testUser");
user1.setPword("testPassword");
session.save(user1);
session.getTransaction().commit();
}
}
You closed the <class> tag. Try this
<class catalog="user" name="beans.UserDetail" table="USERS">
<id column="ID" name="id" type="java.lang.Integer">
<generator class="assigned"/>
</id>
<property column="UNAME" name="uname" type="string"/>
<property column="PWORD" name="pword" type="string"/>
</class>
Related
I'm new with hibernate, after writing my entities, hibernate says that it cannot locate the getters and setters. I got one error at the time, like :
Exception in thread "main" org.hibernate.PropertyNotFoundException: Could not locate getter method for property [entites.Foret#idForet]
at lanceur.Test.main(Test.java:43)
Here is my cfg.xml :
<?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="entites.Foret" table="FORET">
<id name="idForet" type="long" column="idForet">
<generator class="increment" />
</id>
<set name = "emplacement_arbre" table ="ARBRE" lazy ="true" fetch = "select" cascade ="save-update, delete">
<key column = "idForet"/>
<one-to-many class ="entites.Arbre"/>
</set>
<property name="taille_foret" />
<!--<property name="coordonnees" /> -->
<property name="ville" />
<property name="matricule" />
<property name="nom" />
</class>
</hibernate-mapping>
And my foret.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<!-- <property name="hibernate.connection.datasource">jdbc/voteo</property>-->
<property name="hibernate.connection.password">mdp</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Laforet</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.connection.release_mode">after_transaction</property>
<mapping resource="utilisateur.hbm.xml" />
<mapping resource="foret.hbm.xml" />
<mapping resource="arbre.hbm.xml" />
<mapping resource="adresse.hbm.xml" />
</session-factory>
</hibernate-configuration>
Also my java entity:
package entites;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
#Entity
#Table(name = "FORET")
public class Foret {
#GeneratedValue
#Id
private Long idForet;
#OneToMany(mappedBy = "Foret")
private List<Arbre> emplacement_arbre;
private int taille_foret;
private double[] coordonnees;
private String ville;
private String matricule;
private String nom;
public List<Arbre> getEmplacement_arbre() {
return emplacement_arbre;
}
public void setEmplacement_arbre(List<Arbre> emplacement_arbre) {
this.emplacement_arbre = emplacement_arbre;
}
public int getTaille_foret() {
return taille_foret;
}
public void setTaille_foret(int taille_foret) {
this.taille_foret = taille_foret;
}
public Long getIdForet() {
return idForet;
}
public void setIdForet(Long idForet) {
this.idForet = idForet;
}
public double[] getCoordonnees() {
return coordonnees;
}
public void setCoordonnees(double[] coordonnees) {
this.coordonnees = coordonnees;
}
public String getMatricule() {
return matricule;
}
public void setMatricule(String matricule) {
this.matricule = matricule;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
#Override
public String toString() {
return "Foret [emplacement_arbre=" + emplacement_arbre + ", taille_foret=" + taille_foret + ", id=" + idForet
+ ", nom=" + nom + "]";
}
public String getVille() {
return ville;
}
public void setVille(String ville) {
this.ville = ville;
}
}
I checked my javabean naming convention and it seems correct.
Any suggestions ?
package com.simpleprogrammer;
import org.hibernate.Session;
public class Program {
public static void main(String[] args) {
System.out.println("HEY");
Session session = HibernateUtilities.getSessionFactory().openSession();
session.beginTransaction();
User user = new User();
user.setName("pallavi");
user.setGoal(100);
session.getTransaction().commit();
session.close();
HibernateUtilities.getSessionFactory().close();
}
User.java
package com.simpleprogrammer;
public class User {
private int id;
private String name;
private int total;
private int goal;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getGoal() {
return goal;
}
public void setGoal(int goal) {
this.goal = goal;
}
}
HibernateUtilities.java
package com.simpleprogrammer;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtilities {
private static SessionFactory sessionFactory;
private static StandardServiceRegistry serviceRegistry;
static{
try{
Configuration configuration = new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch(HibernateException exception){
System.out.println("Problem creating session factory");
exception.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/*
public static void setSessionFactory(SessionFactory sessionFactory) {
HibernateUtilities.sessionFactory = sessionFactory;
} */
}
Hibernate config file :-----------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">protein_tracker</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.generate_statistics">false</property>
<mapping resource="com/simpleprogrammer/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User mapping file-----------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 1 May, 2017 12:09:45 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.simpleprogrammer.User" table="USERS">
<id name="id" type="int">
<column name="ID" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="total" type="int">
<column name="TOTAL" />
</property>
<property name="goal" type="int">
<column name="GOAL" />
</property>
</class>
</hibernate-mapping>
Everything is working fine but data is not getting stored in database, i dont understand what is wrong!
You create the User entity, but you do not tell the persistence provider to actually manage it and eventually physically store it in the database:
User user = new User();
user.setName("pallavi");
user.setGoal(100);
session.persist(user);
Alternatively you can use save method.
This is the case when creating a new entity. If you were to update an existing entity, which you queries within the same transaction, then you would not need to explicitly call the update or merge operation as the persistence provider would do that for you implicitly on transaction commit.
Yes, I have seen this question all over the place,but am still not able to figure out what is the issue with my code. Certainly am overlooking something, can't just tell what. I could certainly use a second eye.
Here's my POJO.
package hibernate;
public class User {
private int id;
private String name;
private int total;
private int goal;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getGoal() {
return goal;
}
public void setGoal(int goal) {
this.goal = goal;
}
}
Below is my hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=test;</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">admin</property>
<mapping resource="hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Here's my User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 18, 2016 12:10:00 AM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="hibernate.User" table="USER">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="total" type="int">
<column name="TOTAL" />
</property>
<property name="goal" type="int">
<column name="GOAL" />
</property>
</class>
</hibernate-mapping>
Here's the main app putting the above together:
package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class App {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
session.beginTransaction();
User user = new User();
user.setName("Joe");
user.setGoal(250);
session.save(user);
session.getTransaction().commit();
session.close();
HibernateUtility.getSessionFactory().close();
}
}
When i run the above, I get the exception below.
Jan 18, 2016 9:34:04 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
Exception in thread "main" org.hibernate.MappingException: Unknown entity: hibernate.User
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
at hibernate.App.main(App.java:27)
I have seen many people having this issues,but still havent been able to figure out mine. Thanks a bunch for your help.
Your session factory configuration is incorrect for Hibernate 5. If you use Hibernate 5, you can create a session factory by this way
sessionFactory = new Configuration().configure().buildSessionFactory();
In the file User.hbm.xml you shouldn't use namespace
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
Use
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"
instead
I think mistake is on this line:
<class name="hibernate.User" table="USER">
Change hibernate.User to User, it should help.
I am learning hibernate and I can't figure out why is this error popping up. I tried searching but I couldn't find a solution that helped me. I'd like to learn why am I getting this error.
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.simpleprogrammer.User
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1451)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:678)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665)
at Program.main(Program.java:23)
Please help I have already wasted so many hours debugging this.
Here is my simple program,
Program.java
import org.hibernate.Session;
import com.simpleprogrammer.HibernateUtilities;
import com.simpleprogrammer.User;
public class Program {
/**
* #param args
* #throws ClassNotFoundException
* #throws SQLException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World");
Session session = HibernateUtilities.getSessionFactory().openSession();
session.beginTransaction();
User user = new User();
user.setName("Deepak");
user.setTotal(130);
user.setGoal(150);
session.save(user);
session.getTransaction().commit();
session.close();
}
}
HibernateUtilities.java
package com.simpleprogrammer;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtilities {
public static SessionFactory sessionFactory;
public static ServiceRegistry serviceRegistry;
static{
try{
Configuration configuation = new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuation.getProperties()).build();
sessionFactory = configuation.buildSessionFactory(serviceRegistry);
}catch(HibernateException e){
System.out.println("Error while creating Session Factory.");
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
User.java
package com.simpleprogrammer;
public class User {
private int id;
private String name;
private int total;
private int goal;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getGoal() {
return goal;
}
public void setGoal(int goal) {
this.goal = goal;
}
}
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 28, 2015 8:05:49 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.simpleprogrammer.User" table="users">
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="total" type="int">
<column name="total" />
</property>
<property name="goal" type="int">
<column name="goal" />
</property>
</class>
</hibernate-mapping>
hibernate.cfg.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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">protein_tracker</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/simpleprogrammer/User.hbm.xml" />
</session-factory>
Actually I was using Hibernate 5 jars . But the tutorial is actually for Hibernate 4. When I used Hibernate 4 jars everything seems to working fine.
May be you don't need <mapping resource="com/simpleprogrammer/User.hbm.xml" />. Instead, try <mapping resource="User.hbm.xml" />
I have been trying to make a table automatically with hibernate. Tried all the solutions given for the same question. Please have a look and point out what is wrong. I am using it with MySQL 5.6 version.
public class Tester
{
public static void main(String[] args)
{
SessionFactory factory = new Configuration().configure("com/hibernate.hbm.xml").buildSessionFactory();
System.out.println(factory.toString() + " - DONE");
}
}
Product class is :
public class Product
{
private String product;
private String productName;
public String getProduct()
{
return product;
}
public void setProduct(String product)
{
this.product = product;
}
public String getProductName()
{
return productName;
}
public void setProductName(String productName)
{
this.productName = productName;
}
}
My hbm file looks like this :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com">
<class name="Product" table="product_table">
<id name="product" column="product_id" type="java.lang.String" length="20" >
<generator class="assigned" />
</id>
<property name="productName" column="product_name" type="java.lang.String" length="50" not-null="true" />
</class>
</hibernate-mapping>
And hibernate.cfg.xml is given below -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/caching_hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="cache.providerclass">org.hibernate.NoCacheProvider</property>
<mapping resource="com/product.hbm.xml" />
</session-factory>
</hibernate-configuration>
Program shows no exception and runs normally..Thanks in advance