Hi all, I guess I have the same problem of people who post here...
I'm using hibernate 3.2 and hibernate annotation 3.3GA.ù
My database confuguration is:
Code:
CREATE TABLE `mercatino`.`customers` (
`id` int(10) NOT NULL auto_increment,
`type` tinyint(2) default NULL,
`firstname` varchar(20) default NULL,
`lastname` varchar(30) default NULL,
`address` varchar(30) default NULL,
`zip_code` varchar(5) default NULL,
`city` varchar(30) default NULL,
`region` varchar(2) default NULL,
`fiscal_code` varchar(16) default NULL,
`piva` varchar(16) default NULL,
`id_number` varchar(7) default NULL,
`destination_doc` varchar(30) default NULL,
`phone1` varchar(10) default NULL,
`phone2` varchar(10) default NULL,
`mobile` varchar(11) default NULL,
`fax` varchar(11) default NULL,
`email` varchar(35) default NULL,
`payment_notes` varchar(50) default NULL,
`prev_datas` int(10) default NULL,
`delete` int(2) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `mercatino`.`movements` (
`id_product` int(10) NOT NULL,
`id_customer` int(10) NOT NULL,
`type` varchar(10) NOT NULL,
`date` varchar(10) NOT NULL,
`during` int(3) NOT NULL,
`end_description` varchar(30) NOT NULL,
`price` int(10) NOT NULL,
PRIMARY KEY (`id_product`,`id_customer`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `mercatino`.`products` (
`id` int(10) NOT NULL auto_increment,
`description` varchar(50) default NULL,
`notes` varchar(50) default NULL,
`lotto` int(10) default NULL,
`sold` int(1) default '0' COMMENT '0 = not sold, 1 = sold',
`code` varchar(15) default NULL COMMENT 'data + progressive number (maybe id number)',
`provvigion` int(10) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
So, my problem is the bind of composite primary key in movements table to other 2 tables.
here there are my class:
Code:
@Entity
@Table (name ="movements")
/*@AssociationOverrides({
@AssociationOverride(name="id.product", joinColumns = @JoinColumn(name="id_product")),
@AssociationOverride(name="id.customer", joinColumns = @JoinColumn(name="id_customer"))
})*/
//@IdClass(MovementPrimaryKey.class)
public class Movements implements Serializable{
@Id
private MovementPrimaryKey id = new MovementPrimaryKey();
@SuppressWarnings("unused")
@Column(name="id_product", nullable=false, updatable=false, insertable=false)
private Long id_product;
@SuppressWarnings("unused")
@Column(name="id_customer", nullable=false, updatable=false, insertable=false)
private Long id_customer;
private String type;
private String date;
private int during;
private String end_description;
private int price;
public Movements() { }
//some getters and setters
public Customer getId_customer() {
return id.getId_customer();
}
public void setId_customer(Customer id_customer) {
id.setId_customer(id_customer);
}
public Product getId_product() {
return id.getId_product();
}
public void setId_product(Product id_product) {
id.setId_product(id_product);
}
}
@Entity
@Table(name="products")
public class Product implements Serializable {
public Product(){ }
private Long id;
//private String name;
private String description;
//private int buy_price;
//private int sell_price;
private long provvigion;
private String notes;
/*private String cod_end_contract;
private String description_end_contract;
private String start_data;
private int during;*/
//private String product_type;
private String code;
private int sold;
private Lotto lotto;
/*private Set customers = new HashSet();
private Set provvigions = new HashSet();
private Set lottos = new HashSet();
private Set productTypes = new HashSet();*/
private List<Movements> movements = new ArrayList();
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@ManyToOne
public Long getId() {
return id;
}
protected void setId(Long id) {
this.id = id;
}
//some getters and setters
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, targetEntity=net.bayzone.mercatino.tables.Lotto.class )
@JoinColumn(name="lotto")
public Lotto getLotto() {
return lotto;
}
public void setLotto(Lotto lotto) {
this.lotto = lotto;
}
public boolean equals(Object other) {
if (this == other) return true;
if ( !(other instanceof Product) ) return false;
final Product cat = (Product) other;
if ( !cat.getId().equals( getId() ) ) return false;
return true;
}
public int hashCode() {
int result;
result = getId().hashCode();
result = 29 * result + Integer.parseInt(getId().toString());
return result;
}
@OneToMany(mappedBy="id.id_product")
public List<Movements> getMovements() {
return movements;
}
public void setMovements(List movements) {
this.movements = movements;
}
}
@Entity
@Table(name="customers")
public class Customer implements Serializable{
public Customer() { }
private Long id;
private int type;
private String firstname;
private String lastname;
private String address;
private String zip_code;
private String city;
private String region;
private String fiscal_code;
private String piva;
private String id_number;
private String destination_doc;
private String phone1;
private String phone2;
private String mobile;
private String fax;
private String email;
private String payment_notes;
private Long prev_datas;
private List<Movements> movements= new ArrayList();
//private Set products = new HashSet();
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@ManyToOne
public Long getId() {
return id;
}
protected void setId(Long id) {
this.id = id;
}
//some getters and setters
@OneToMany(mappedBy="id.id_customer")
public List<Movements> getMovements() {
return movements;
}
public void setMovements(List movements) {
this.movements = movements;
}
}
There is also another table called lottos but is not important here.If you think is important to resolve my problem, tell me and I will post here.
so, all the file work, but when I try to insert data, I receive this error:
Code:
Hibernate: insert into movements (date, during, end_description, price, type, id_customer, id_product) values (?, ?, ?, ?, ?, ?, ?)
Sep 21, 2007 4:51:28 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1366, SQLState: HY000
Sep 21, 2007 4:51:28 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Incorrect integer value: '’' for column 'id_customer' at row 1
Someone can help me? I'm becaming fool...
Thanks a lot in advance..Tell me if you need some information...
bye
Luca