zhangjingl02 / activejdbc

Automatically exported from code.google.com/p/activejdbc
0 stars 0 forks source link

.save() method throws error #189

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. create a table with composite key as primary key: for example:
CREATE TABLE `segments` (
  `segment_id` bigint(20) NOT NULL,
  `domain_id` int(11) NOT NULL,
  PRIMARY KEY (`segment_id`,`domain_id`)
2. create an activeJDBC model 
public class Segment extends Model { ...

3. insert the same primary key twice through the following code
              for(int i=0;i<2;i++) {
        Segment segment = new Segment();
        segment.setLong("segment_id", segmentId);
        segment.setInteger("domain_id", domainId);
        segment.save();
              }
4. .save() function reached exception at the second time.

What is the expected output? What do you see instead?

 according to the documentation:  the method save() will silently exit without throwing exceptions..

 and I reached exception:

What version of the product are you using? On what operating system?
  1.4.6

Exception message:

org.javalite.activejdbc.DBException: 
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 
Duplicate entry '100-1' for key 'PRIMARY', Query: INSERT INTO segments 
(segment_id, domain_id) VALUES (?, ?), params: 100,1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
    at org.javalite.activejdbc.DB.execInsert(DB.java:516)
    at org.javalite.activejdbc.Model.doInsert(Model.java:2144)
    at org.javalite.activejdbc.Model.save(Model.java:2051)
    at my.model.Segment.save(Segment.java)
    at my.model.Segment.setDomainOnSegment(Segment.java:35)
    at my.model.Segment_JUnit.test(Segment_JUnit.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: 
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 
Duplicate entry '100-1' for key 'PRIMARY'
    ... 46 more

Original issue reported on code.google.com by yzh...@onescreen.com on 1 Mar 2013 at 11:16

GoogleCodeExporter commented 9 years ago
Method save() will not throw exceptions only in case of validation exceptions. 
Validations are described on page: 
http://code.google.com/p/activejdbc/wiki/Validations

ActiveJDBC does not manage composite PKs it is a developers responsibility to 
do this. 
Framework works as expected.
Please, refer to page: 
http://code.google.com/p/activejdbc/wiki/SurrogatePrimaryKeys
for understanding of PKs in AJ

Original comment by i...@polevoy.org on 1 Mar 2013 at 11:23

GoogleCodeExporter commented 9 years ago
if things are still not clear, ask a question on Google Group:
https://groups.google.com/forum/?fromgroups#!forum/activejdbc-group

Original comment by i...@polevoy.org on 2 Mar 2013 at 12:59

GoogleCodeExporter commented 9 years ago
I see. I misunderstood the documentation. thanks I will handle the exception 
myself.

Original comment by yzh...@onescreen.com on 2 Mar 2013 at 2:04