I am new to Spring Boot and Vaadin. I followed a tutorial to create CRUD pages for a phone book application however I am having trouble using foreign keys. I have a Contact table which has phone type (i.e. cell or home) as a foreign key - i.e. it is referenced to my PhoneType table. I am stuck on how to populate the phone type from a drop down of values populated in my PhoneType table. Right now I am I have the following member variable in my Contact class
#ManyToOne
#JoinColumn(name="type")
private PhoneType phoneType;
And in my PhoneType class I have
#Column(name = "type")
private String phoneType;
However I am getting an error that says "Error executing DDL via JDBC Statement".
The rest of the application works well with the CRUD pages.
Firstly in mySQL implementations you can't store actual objects unless you use 8.0+ JSON data type. SQL has no idea what a PhoneType is because it's an object and not a valid data type. https://www.w3schools.com/sql/sql_datatypes.asp
If you want to store actual objects you need to find a noSQL implementation that you like.
So your "Customer" class doesn't map to a table properly. You would need to make instance variables such as
String hasCellPhone, hasHomePhone; //etc for the options in your dropdown menu
instead of trying to put a phonetype object.
I asked almost the exact same question, I suggest you read this entire thread.
https://stackoverflow.com/a/50879597/5468597
create table item (
barcode bigint not null auto_increment primary key,
name varchar(20) not null,
type varchar(20) not null,
is_available boolean not null,
is_late boolean null,
notes varchar(255) null,
check_out_date datetime null,
due_date datetime null
#create index idx_barcode (barcode));
create table patron (
trinity_id bigint not null primary key,
name varchar(30) not null,
email varchar(20) not null,
owes_fines boolean not null,
fines_owed int null
#create index idx_trinity_id (trinity_id));
create table checked_out_items (
ref_id bigint primary key auto_increment not null,
patron_id bigint not null,
item_id bigint not null,
item_available boolean not null,
item_check_out_date datetime null,
item_due_date datetime null);
alter table checked_out_items
add constraint fk_patron_id
foreign key (patron_id) references patron(trinity_id),
add constraint fk_item_id
foreign key (item_id) references item(barcode)
#add constraint fk_item_available
#add constraint fk_check_out_date
#add constraint fk_due_date
#foreign key (item_available references item(is_available)
#foreign key (item_check_out_date) references item(check_out_date)
#foreign key (item_due_date) references item(due_date)
on update cascade
on delete cascade;
insert into patron values(0000000,'Test Erino','test#erino.edu',0,null);
insert into item values(1,'Chromebook','Laptop',0,null,null,null,null);
insert into checked_out_items(patron_id,item_id,item_available,item_check_out_date,item_due_date)
select patron.trinity_id,item.barcode,item.is_available,item.check_out_date,item.due_date
from patron
inner join item;
and lastly:
select * from item;
select * from patron;
select * from checked_out_items;
I won't post the java logic here. That's for you to read in the other thread.
I solved my question.
#ManyToOne (cascade = {CascadeType.ALL})
#JoinColumn(name="phoneType_typeId")
private PhoneType phoneType;
And
#Id
#GeneratedValue
#Column(name = "typeId")
private Long id;
Related
I tried to update the table by jpa.
CREATE TABLE `MagicnotifyCart` (
`uuid` VARCHAR(50),
`userid` BIGINT(20),
`desired_price` DECIMAL(10,2),
FOREIGN KEY (`uuid`) REFERENCES `MagicnotifyUuidName` (`key`),
FOREIGN KEY (`userid`) REFERENCES `User` (`id`)
);
the table i wanted to update is this one, and i tried to update it by jpa.
#Transactional
public void updateDesiredPrice(BigDecimal desired_price, String uuid, int userid){
MagicnotifyCart magicnotifyCart = cartRepository.findMagicnotifyCartById_UuidAndId_Userid(uuid, userid);
magicnotifyCart.getId().setDesired_price(desired_price);
cartRepository.save(magicnotifyCart);
}
by using this, i expect to update desired_price null to desired_price, but it won't update when i try this code.
I debugged the code and I see magicnotifyCart.getId().getDesired_price() becomes the price as I expected, but when it comes to save the value it won't update.
What should I do if I want to update the value of desired_price by using jpa?
There is a possibility that magicnotifyCart.getId() returns a different/new detached MagicnotifyCart instance, the other magicnotifyCart is already non-transient instance of magicnotifyCart so ideally you would just need :
magicnotifyCart.setDesired_price(desired_price);
Hello together I am currently trying to persist an entity to the database with a 1 to 1 relationship.
I am using spring version 2.5.3 (spring-boot-data-jdbc).
When I try to persist via rest post and the usual crud repository save() method with this json.
{
"name": "everyday at 15",
"announcement": {
"name": "This is the third announcement"
}
}
I get this error message:
2021-08-19 14:20:06.315 ERROR 7946 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.relational.core.conversion.DbActionExecutionException: Failed to execute DbAction.InsertRoot(entity=Timetable(id=null, name=everyday at 15, announcement=Announcement(id=null, name=This is the third announcement)))] with root cause
org.postgresql.util.PSQLException: ERROR: insert or update on table "timetable" violates foreign key constraint "fk_announcement"
Detail: Key (announcement)=(6) is not present in table "announcement".
It seems like the counter for the announcement id is always going up however it never reaches the point where anything is persisted.
My entity and db setup are listed below.
#Table("announcement")
data class Announcement(
#Id
val announcement_id: Long?,
val name: String
)
#Table("timetable")
data class Timetable(
#Id
var id: Long?,
val name: String,
val announcement: Announcement
)
CREATE TABLE announcement(
announcement_id serial,
name varchar(30) not null,
PRIMARY KEY(id)
);
CREATE TABLE timetable(
id serial,
name varchar(30) not null,
announcement_id serial,
PRIMARY KEY(id),
CONSTRAINT fk_announcement
FOREIGN KEY (announcement_id)
REFERENCES announcement (announcement_id)
);
Thank you for your help!
Since Timetable is your aggregate root, the foreign key should be placed on the announcement table, not on the timetable table.
Note that Announcement.announcement_id is superfluous as far as Spring Data JDBC is concerned. In the following schema I left it in but put the primary key on the new timetable column:
CREATE TABLE announcement(
announcement_id serial,
timetable biginteger, -- the correct type to use depends on the database you use.
name varchar(30) not null,
PRIMARY KEY(timetable),
CONSTRAINT fk_announcement_timetable
FOREIGN KEY (timetable)
REFERENCES timetable (announcement_id)
);
CREATE TABLE timetable(
id serial,
name varchar(30) not null,
PRIMARY KEY(id)
);
I am having issues with Go's GORM. When I am trying to save an entity to the DB with a model inside it, it does not save the foreign key with the owner model.
Below is my Models, The MySQL script and the way I save/create the model into the DB.
The error I am getting is: Field 'business_industry_id' doesn't have a default value
type Business struct {
gorm.Model
BusinessName string `json:"BusinessName" binding:"required" gorm:"column:business_name;type:varchar(100);unique;not null"`
BusinessIndustry Industry `json:"BusinessIndustry" binding:"required" gorm:"foreignkey:id"`
}
type Industry struct {
gorm.Model
Name string `json:"Name" gorm:"column:name;type:varchar(100);unique;not null"`
}
CREATE TABLE `industries`
(
id INT(6) AUTO_INCREMENT,
name VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
deleted_at TIMESTAMP,
updated_at TIMESTAMP,
PRIMARY KEY (id)
);
CREATE TABLE `businesses`
(
id INT(6) AUTO_INCREMENT,
business_name VARCHAR(100) NOT NULL UNIQUE,
business_industry_id INT(6) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
deleted_at TIMESTAMP,
updated_at TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (business_industry_id) REFERENCES industries (id)
);
err := bs.database.Create(business).Error
I tried remove the Grom attributes from the models, to let the framework figure it out on it's own, but I got the same error.
When I inspect the model, the industry have id of 3 (Because I resolved it earlier on myself) and after I do the save, the ID is 0.
But when I removed the attributes, the id was 3 after the save as well, but the same error occurred.
I know what the error message mean, because the sql message that is logged, doesn't actually insert the 3 into the business_industry_id field. What I don't know is, why it doesn't insert it.
I'm fairly certain you have to include the foreign key, you can't just have the associated model (see http://gorm.io/docs/has_many.html). So you need to do:
type Business struct {
gorm.Model
BusinessName string `json:"BusinessName" binding:"required" gorm:"column:business_name;type:varchar(100);unique;not null"`
BusinessIndustryID uint
BusinessIndustry Industry `json:"BusinessIndustry" binding:"required" gorm:"foreignkey:id"`
}
I am using JOOQ with Spring. I have table like this:
CREATE TABLE city (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB;
From this table JOOQ generates
#NotNull
public UInteger getId() {
return this.id;
}
This will fail Javax validation #Valid.
There was google group Link and it was said it was fixed Link.
I am using JOOQ 3.9.1.
Am I doing something incorrectly?
Indeed, it seems that fix #5128 (which you've linked) only works for those databases that report identity columns as defaulted columns to jOOQ-meta.
An additional fix will be required to exclude also "non-defaulted" identity columns from having this annotation generated:
https://github.com/jOOQ/jOOQ/issues/6401
I am creating a component for Joomla! 2.5 and I have the following MySQL table structure.
CREATE TABLE `#__table_a` (
id INT NOT NULL AUTO_INCREMENT,
actividad VARCHAR( 255 ),
publish_up DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
publish_down DATETIME NOT NULL
);
CREATE TABLE `#__table_b` (
id INT NOT NULL AUTO_INCREMENT,
table_a_id INT NOT NULL, /*foreign key to table '#__table_a' field 'id'*/
hora TIME NOT NULL,
created DATETIME NOT NULL,
publish_up DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
publish_down DATETIME NOT NULL
);
What I want is that, in the form where I managed data from "# __table_a", I need to be able to manage data that is listed in the table "#__table_b".
How do I do it?,
Ie where do I do the "query" for such data and to display them in the form?, in the "table_a" model or controller?