java - At a complete loss: mapping a simple entity via Hibernate -
i can't figure out why object not map via annotations. no matter try, receive error:
org.hibernate.mappingexception: unknown entity: com.hibernate.practice.car
i loosely following tutorial here can't seem working. i've tried strip object down bare bones (thinking making error in code somewhere), again, id, , name column, still fail working.
my hibernate.cfg
netbeans hibernate tutorial.
<?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.url">jdbc:mysql://localhost:3306/practicedb?zerodatetimebehavior=converttonull</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">pass</property> <property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">create</property> <property name="hibernate.current_session_context_class">thread</property> </session-factory> </hibernate-configuration>
also, per instructions, i've added hibernateutil.java class.
public class hibernateutil { private static final sessionfactory sessionfactory; static { try { // create sessionfactory standard (hibernate.cfg.xml) // config file. sessionfactory = new annotationconfiguration().configure().addpackage("com.hibernate.practice").buildsessionfactory(); } catch (throwable ex) { // log exception. system.err.println("initial sessionfactory creation failed." + ex); throw new exceptionininitializererror(ex); } } public static sessionfactory getsessionfactory() { return sessionfactory; } }
i set simple class. it's car
id , name.
@entity public class car { @id @generatedvalue @genericgenerator(name="increment", strategy="incrememnt") private long id; @column(name="car_name") private string name; public car() { } public long getid() { return id; } public void setid(long id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
and finally, i'm trying test stuff out this:
@test public void testouthibernate() { sessionfactory factory = hibernateutil.getsessionfactory(); session session = factory.opensession(); car car = new car(); car.setname("red one"); session.save(car); try { transaction t = session.begintransaction(); session.save(car); t.commit(); } { session.close(); } session.close(); }
without fail unknown entity exception
. there glaringly obvious i'm missing?
you need add annotated class follows -
sessionfactory = new annotationconfiguration() .addannotatedclass(car.class) .addpackage("com.hibernate.practice") .configure() .buildsessionfactory();
you can find discussion regarding addpackage()
method here - https://forum.hibernate.org/viewtopic.php?f=1&t=980723.
Comments
Post a Comment