1

I have 3 tables - Rasskazi (main table), CatsRelations (relations between table Rasskazi and table CatsNames) and CatsNames.

CREATE TABLE `rasskazi` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `rasskazName` varchar(777) DEFAULT NULL,
  `rasskazText` text,
  `rasskazDataDobav` datetime DEFAULT NULL,
  `rasskazRazmer` float DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

CREATE TABLE `catsRelations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `rasskazId` int(11) DEFAULT NULL,
  `catId` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `rasskazIdFK_idx` (`rasskazId`),
  KEY `catIdFK_idx` (`catId`),
  CONSTRAINT `z1` FOREIGN KEY (`rasskazId`) REFERENCES `rasskazi` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `z2` FOREIGN KEY (`catId`) REFERENCES `catsNames` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `catsNames` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `catName` varchar(777) DEFAULT NULL,
  `oldId` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `oldId_UNIQUE` (`oldId`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;

Hibernate objects are:

Rasskazi.hbm.xml [http://pastebin.com/CF4D47M1][1]
Rasskazi.java [http://pastebin.com/29phnEEd][2]
CatsRelations.hbm.xml [http://pastebin.com/MqnXuAQ9][3]
CatsRelations.java [http://pastebin.com/BDqTKqeD][4]
CatsNames.hbm.xml [http://pastebin.com/9YtVkCjD][5]
CatsNames.java [http://pastebin.com/uwpWcLha][6]

All files packed to the zip and can be download here

I try to save objects by code

Rasskazi r = new Rasskazi();
    r.setRasskazName(storyName);
    Set<CatsRelations> catsRelationses = new HashSet<>();
    Elements katsInfo = doc.select("a[href*=ras.shtml?kat]");
    for (Element kat : katsInfo) {
        String katId = kat.attr("href");
        CatsNames cat = mainBean.getCat(katId); // Here I got CatsNames from my bean, from table named CatsNames

        CatsRelations catsRelations = new CatsRelations();
        catsRelations.setCatsNames(cat);
        catsRelations.setRasskazi(r);

        catsRelationses.add(catsRelations);

    }
    r.setCatsRelationses(catsRelationses);
    r.setRasskazText(textStr);
    Session session = sessionFactory.getCurrentSession();
    session.save(r);

But Hibernate saves record only in table named rasskazi:

Hibernate: 
    insert 
    into
        grabberRasskazov.rasskazi
        (rasskazName, rasskazText, rasskazDataDobav, rasskazRazmer) 
    values
        (?, ?, ?, ?)

And it doesn't save any data to the table named CatsNames. Why it doesn't save data to the table CatsNames, why it ignored the code

CatsRelations catsRelations = new CatsRelations();
catsRelations.setCatsNames(cat);
catsRelations.setRasskazi(r);

catsRelationses.add(catsRelations);

}
r.setCatsRelationses(catsRelationses);

?

1 Answer 1

2

The problem is that you don't cascade from Parent to Children entities:

<set name="children" inverse="true" cascade="all">
    <key column="parent_id"/>
    <one-to-many class="Child"/>
</set>

Make sure you add cascade to all one-to-many associations.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.