UPDATE 2014-01-29: I have changed relationshis so entity code and error changed too
I have to store some data in a database and my relations are like this: Order3d 1-----* Draw 1-----* Line I want to save everything like this:
Order3d o=new Order3d();
//add data to order (lines and draws)
Session s=HibernateUtils.getSessionFactory().openSession();
s.save(o);
s.close();
I don't have runtime error but now my line is not inserted (only order3d and drawfile are saved)
here are my entities (obviously, they are POJO so I didn't copy getters and setters):
@Entity
@Table (name="order3d")
public class Order3d implements Serializable {
private static final long serialVersionUID = -2241346447352903470L;
public enum State {DEMAND, ESTIMATED, PAYED, PENDING, PRODUCED, SENT, DELIVERED};
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column (name="id")
private int id;
@Column (name="person")
private int person;
@Column (name="state", columnDefinition="smallint")
private State state;
@Column (name="labor_expense")
private float laborExpense=0;
@Column (name="travel_expense")
private float travelExpense=0;
@Column (name="validity_date")
private Date validityDate;
@Column (name="demand_date")
private Date demandDate;
@Column (name="estimate_date")
private Date estimateDate;
@Column (name="order_date")
private Date orderDate=null;
@Column (name="modification", columnDefinition="TEXT")
private String modification;
@Column (name="delivery", columnDefinition = "BIT", length = 1)
private Boolean delivery=true;
@OneToMany(mappedBy="order3d", cascade=CascadeType.ALL)
private Set<DrawFile> myDraws=new HashSet<DrawFile>(5);
public void addDrawFile(DrawFile df) {
df.setOrder3d(this);
myDraws.add(df);
}
}
@Entity
@Table (name="draw")
public class DrawFile implements Serializable {
private static final long serialVersionUID = -9024754876558087847L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column (name="id")
private int id;
@Column (name="hashname", columnDefinition="CHAR(64)")
private String hashname;
@Column (name="filename")
private String filename="";
@Column (name="accepted", columnDefinition = "BIT", length = 1)
private Boolean accepted=true;
@Column (name="format")
private String format;
@Column (name="size")
private int size;
@Column(name="width")
short width;
@Column(name="depth")
short depth;
@Column(name="height")
short height;
@OneToMany(mappedBy="draw", cascade=CascadeType.ALL)
private Set<Order3dLine> myLines=new HashSet<Order3dLine>(5);
@ManyToOne
@JoinColumn(name="order3d_id")
private Order3d order3d;
public void addLine(Order3dLine l) {
l.setDraw(this);
myLines.add(l);
}
}
@Entity
@Table (name="line3d")
public class Order3dLine implements Serializable {
private static final long serialVersionUID = 3993578603382571145L;
@NaturalId
@ManyToOne
@JoinColumn(name="draw_id")
private DrawFile draw;
@Column (name="quantity")
private short quantity=0;
@Id
@Column (name="material")
private String material;
@Id
@Column (name="color")
private int color;
@Column(name="produced")
short produced=0;
@Column(name="duration")
short duration=0;
}
Here are my MySQL tables:
CREATE TABLE `draw` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`filename` varchar(255) DEFAULT NULL,
`format` varchar(6) DEFAULT NULL,
`hashname` char(64) DEFAULT NULL,
`accepted` bit(1) NOT NULL DEFAULT b'0',
`size` int(11) NOT NULL DEFAULT '0',
`order3d_id` int(11) NOT NULL,
`width` smallint(6) NOT NULL DEFAULT '0',
`depth` smallint(6) NOT NULL DEFAULT '0',
`height` smallint(6) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
CONSTRAINT `fk_draw_order3d` FOREIGN KEY (`id`) REFERENCES `order3d` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf-8;
/*!40101 SET character_set_client = @saved_cs_client */;
CREATE TABLE `line3d` (
`draw_id` int(11) NOT NULL,
`material` varchar(10) NOT NULL,
`color` int(11) NOT NULL,
`quantity` smallint(6) NOT NULL DEFAULT '0',
`produced` smallint(6) NOT NULL DEFAULT '0',
`duration` smallint(6) NOT NULL DEFAULT '0',
`layers` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`draw_id`,`material`,`color`),
CONSTRAINT `fk_line_draw` FOREIGN KEY (`draw_id`) REFERENCES `draw` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf-8;
CREATE TABLE `order3d` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`person` int(11) DEFAULT NULL,
`state` smallint(6) DEFAULT NULL,
`labor_expense` float DEFAULT NULL,
`travel_expense` float DEFAULT NULL,
`validity_date` date DEFAULT NULL,
`estimate_date` date DEFAULT NULL,
`order_date` date DEFAULT NULL,
`delivery` bit(1) NOT NULL DEFAULT b'0',
`modification` text,
`demand_date` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_order_user` (`person`),
CONSTRAINT `fk_order_user` FOREIGN KEY (`person`) REFERENCES `person` (`id`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf-8;