mysql - Hibernate not inserting rows -
i able generate schema when try insert, nothing seems happening. won't print in logs either. phone table primary key composite key consisting of phonenumber , foreign key id.
i have classes below
student.java
import java.io.serializable; import java.util.set; import javax.persistence.cascadetype; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.onetomany; @entity @suppresswarnings("serial") public class student implements serializable { @id @generatedvalue(strategy = generationtype.auto) private int id; private string fname; private string lname; private string mname; @onetomany(cascade = cascadetype.all) @joincolumn(name = "id") private set<phone> phones; /** * @return fname */ public string getfname() { return fname; } /** * @return id */ public int getid() { return id; } /** * @return lname */ public string getlname() { return lname; } /** * @return mname */ public string getmname() { return mname; } /** * @return phones */ public set<phone> getphones() { return phones; } /** * @param fname * fname set */ public void setfname(final string fname) { this.fname = fname; } /** * @param id * id set */ public void setid(final int id) { this.id = id; } /** * @param lname * lname set */ public void setlname(final string lname) { this.lname = lname; } /** * @param mname * mname set */ public void setmname(final string mname) { this.mname = mname; } /** * @param phones * phones set */ public void setphones(final set<phone> phones) { this.phones = phones; } }
phone.java
import java.io.serializable; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.idclass; import javax.persistence.joincolumn; import javax.persistence.manytoone; @idclass(phonepk.class) @entity @suppresswarnings("serial") public class phone implements serializable { @id private string phonenumber; @id @manytoone @joincolumn(name = "id", insertable = false, updatable = false) private student student; private string color; /** * @return color */ public string getcolor() { return color; } /** * @return phonenumber */ public string getphonenumber() { return phonenumber; } /** * @return student */ public student getstudent() { return student; } /** * @param color * color set */ public void setcolor(final string color) { this.color = color; } /** * @param phonenumber * phonenumber set */ public void setphonenumber(final string phonenumber) { this.phonenumber = phonenumber; } /** * @param student * student set */ public void setstudent(final student student) { this.student = student; } }
phonepk.java
import java.io.serializable; @suppresswarnings("serial") public class phonepk implements serializable { private string phonenumber; private student student; /** * @return phonenumber */ public string getphonenumber() { return phonenumber; } /** * @return student */ public student getstudent() { return student; } /** * @param phonenumber * phonenumber set */ public void setphonenumber(final string phonenumber) { this.phonenumber = phonenumber; } /** * @param student * student set */ public void setstudent(final student student) { this.student = student; } }
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">pwd</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <property name="hibernate.connection.username">user</property> <property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property> <property name="connection.pool_size">1</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">create</property> <property name="hibernate.show_sql">true</property> </session-factory> </hibernate-configuration>
main.java
import static org.junit.assert.asserttrue; import java.util.linkedhashset; import java.util.set; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.cfg.configuration; public class main { public static void main(final string args[]) { configuration configuration = new configuration(); configuration.addannotatedclass(student.class); configuration.addannotatedclass(phone.class); configuration.addannotatedclass(phonepk.class); configuration.configure("hibernate.cfg.xml"); sessionfactory sessionfactory = configuration.buildsessionfactory(); session session = sessionfactory.opensession(); student student = new student(); student.setfname("bob"); student.setlname("buster"); set<phone> phones = new linkedhashset<phone>(); phone ph1 = new phone(); ph1.setcolor("black"); ph1.setphonenumber("1111111111"); phone ph2 = new phone(); ph2.setcolor("blue"); ph2.setphonenumber("2222222222"); phones.add(ph1); phones.add(ph2); student.setphones(phones); session.save(student); } }
console output:
jun 29, 2013 10:47:42 org.hibernate.annotations.common.version <clinit> info: hcann000001: hibernate commons annotations {4.0.2.final} jun 29, 2013 10:47:42 org.hibernate.version logversion info: hhh000412: hibernate core {4.2.2.final} jun 29, 2013 10:47:42 org.hibernate.cfg.environment <clinit> info: hhh000206: hibernate.properties not found jun 29, 2013 10:47:42 org.hibernate.cfg.environment buildbytecodeprovider info: hhh000021: bytecode provider name : javassist jun 29, 2013 10:47:42 org.hibernate.cfg.configuration configure info: hhh000043: configuring resource: hibernate.cfg.xml jun 29, 2013 10:47:42 org.hibernate.cfg.configuration getconfigurationinputstream info: hhh000040: configuration resource: hibernate.cfg.xml jun 29, 2013 10:47:42 org.hibernate.internal.util.xml.dtdentityresolver resolveentity warn: hhh000223: recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. use namespace http://www.hibernate.org/dtd/ instead. refer hibernate 3.6 migration guide! jun 29, 2013 10:47:42 org.hibernate.cfg.configuration doconfigure info: hhh000041: configured sessionfactory: null jun 29, 2013 10:47:42 org.hibernate.service.jdbc.connections.internal.drivermanagerconnectionproviderimpl configure info: hhh000402: using hibernate built-in connection pool (not production use!) jun 29, 2013 10:47:42 org.hibernate.service.jdbc.connections.internal.drivermanagerconnectionproviderimpl configure info: hhh000115: hibernate connection pool size: 1 jun 29, 2013 10:47:42 org.hibernate.service.jdbc.connections.internal.drivermanagerconnectionproviderimpl configure info: hhh000006: autocommit mode: false jun 29, 2013 10:47:42 org.hibernate.service.jdbc.connections.internal.drivermanagerconnectionproviderimpl configure info: hhh000401: using driver [com.mysql.jdbc.driver] @ url [jdbc:mysql://localhost:3306/test] jun 29, 2013 10:47:42 org.hibernate.service.jdbc.connections.internal.drivermanagerconnectionproviderimpl configure info: hhh000046: connection properties: {user=user, password=****} jun 29, 2013 10:47:43 org.hibernate.dialect.dialect <init> info: hhh000400: using dialect: org.hibernate.dialect.mysqldialect jun 29, 2013 10:47:44 org.hibernate.mapping.rootclass checkcompositeidentifier warn: hhh000038: composite-id class not override equals(): phonepk jun 29, 2013 10:47:44 org.hibernate.mapping.rootclass checkcompositeidentifier warn: hhh000039: composite-id class not override hashcode(): phonepk jun 29, 2013 10:47:44 org.hibernate.engine.transaction.internal.transactionfactoryinitiator initiateservice info: hhh000399: using default transaction strategy (direct jdbc transactions) jun 29, 2013 10:47:44 org.hibernate.hql.internal.ast.astquerytranslatorfactory <init> info: hhh000397: using astquerytranslatorfactory jun 29, 2013 10:47:44 org.hibernate.tool.hbm2ddl.schemaexport execute info: hhh000227: running hbm2ddl schema export hibernate: alter table phone drop foreign key fk_aoj0eivd0ap3drxnoyk4xj10q hibernate: drop table if exists phone hibernate: drop table if exists student hibernate: create table phone (phonenumber varchar(255) not null, color varchar(255), id integer not null, primary key (phonenumber, id)) hibernate: create table student (id integer not null auto_increment, fname varchar(255), lname varchar(255), mname varchar(255), primary key (id)) hibernate: alter table phone add index fk_aoj0eivd0ap3drxnoyk4xj10q (id), add constraint fk_aoj0eivd0ap3drxnoyk4xj10q foreign key (id) references student (id) jun 29, 2013 10:47:45 org.hibernate.tool.hbm2ddl.schemaexport execute info: hhh000230: schema export complete hibernate: insert student (fname, lname, mname) values (?, ?, ?) hibernate: select phone_.phonenumber, phone_.id, phone_.color color2_0_ phone phone_ phone_.phonenumber=? , phone_.id=? hibernate: select phone_.phonenumber, phone_.id, phone_.color color2_0_ phone phone_ phone_.phonenumber=? , phone_.id=?
replace code below:
try { transaction = session.begintransaction(); session.save(student); transaction.commit(); asserttrue(true); } catch (hibernateexception e) { transaction.rollback(); e.printstacktrace(); } { session.close(); }
since not committing transactions, see goes fine in logs operations performed not committed.
Comments
Post a Comment