Reputation: 349
I'm using Hibernate in combination with phpmyadmin (MySQL). Recently I found a really weird error. Whenever I try to insert a new row (.persist) I get the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'user_id' cannot be null
The error is really obvious. But the weird part is, this error came out of nowhere. I'm ENTIRELY sure the property userId (mapped to 'user_id') isn't null. I tested several times. This is the mapping part: (On the @manytoone part).
<property name="userId" update="false" insert="false" column="user_id" type="java.lang.Long" />
The OneToMany part isn't the problem I guess.
So the problem here is, i'm 100% sure the value isn't null, still Hibernate passes it as null to the MySQL. (Getter does work).
UPDATE
@ManyToOne side, which causes the error
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="xx.xx.xx.AlarmEntity" table="alarms">
<meta attribute="class-description">
Hierin staan de object properties van de alarm entiteit.
</meta>
<id name="id" type="java.lang.Long" column="id">
<generator class="native"/>
</id>
<property name="keepsRunning" type="java.lang.Boolean">
<column name="keeps_running" sql-type="int"></column>
</property>
<property name="userId" update="false" insert="false" column="user_id" type="java.lang.Long" />
<many-to-one name="userAlarm" cascade="save-update" fetch="select" column="user_id" class="xx.xx.xx.UserEntity" />
</class>
</hibernate-mapping>
Upvotes: 7
Views: 12935
Reputation: 1803
Providing Annotation Approach one to Many Example which worked for me
In below example Factory can have multiple product ,that means there is one to many mapping between Factory and Product where Product is owning side of this relationship which means Product will maintain foreign key constraint for holding factory id.
Table structure:
CREATE TABLE `factory` (
`id` bigint NOT NULL AUTO_INCREMENT,
`factory_name` varchar(500) DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `product` (
`id` bigint NOT NULL AUTO_INCREMENT,
`factory_id` bigint NOT NULL ,
`product_name` varchar(500) DEFAULT NULL,
PRIMARY KEY (`id`)
);
ALTER TABLE product
ADD CONSTRAINT FK_product_factory
FOREIGN KEY (factory_id) REFERENCES factory (id);
Entity Structure:
@Entity
@Table(name = “factory”)
public class Factory {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name=“id”)
private int id;
@Column(name = "factory_name")
private String factoryName;
@OneToMany(mappedBy = “factory”,cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Product> products = new ArrayList<>();
}
@Entity
@Table(name = “product”)
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = “product_name")
private String productName;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name=“factory_id")
private Factory factory;
}
Service Structure:
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
@Autowired
private FactoryRepository factoryRepository;
public void saveFactoryAndProduct(){
Factory factory = new Factory();
factory.setFactoryName("F1");
Product product1 = new Product();
product1.setProductName("P1");
factory.getProducts.add(product1);
product.setFactory(factory);
Product product2 = new Product();
product2.setProductName("P2");
factory.getProducts.add(product2);
product1.setFactory(factory);
//Saving factory will also saved linked product1 and product2 data having factory_id as a foreign key from Factory table
factoryRepository.save(factory);
}
}
Note: Saving Products with Mapped Factory.Here we don't need to save product specifically since Factory is maintaining cascade so when we save factory related products will also save but we just need to setFatory for every product object inorder to maintain foreign key in product.
Upvotes: 0
Reputation: 986
Please check the mapping
UserEntity
private int user_id;
private Set<AlarmEntity> alarm;
AlarmEntity
private int alarm_id;
private String keepsRunning;
As per your requirement , I have provided mapping with annotation please check
User
@Id
@Column(name = "userId")
private Long userId;
// parent to Alarm
@Fetch(value = FetchMode.SELECT)
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "userId")
@JsonIgnore
private List<Alarm> alarmList ;
Alarm
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "alarmId")
private Long alarmId;
@ManyToOne
@JoinColumn(name = "userId", insertable = true, updatable = true, nullable = true)
private User user;
@Column(name="keepsRunning")
private String keepsRunning;
Upvotes: 2