In NetBeans7 when I'm trying to add "Entity Classes from Database" the following problem occurs: some of the tables on the left are greyed out and marked "(no primary keys)", so I can't add them.
Actually, the primary keys are correct. The tables groups, groupsyear, specialization are greyed, all others are fine.
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL';
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Faculty`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Faculty` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Faculty` (
`idFaculty` INT NOT NULL ,
`name` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idFaculty`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Specialization`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Specialization` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Specialization` (
`idSpecialization` INT NOT NULL ,
`Faculty_idFaculty` INT NOT NULL ,
`Name` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idSpecialization`) ,
CONSTRAINT `fk_Specialization_Faculty`
FOREIGN KEY (`Faculty_idFaculty` )
REFERENCES `mydb`.`Faculty` (`idFaculty` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Specialization_Faculty` ON `mydb`.`Specialization` (`Faculty_idFaculty` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Student`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Student` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Student` (
`idStudent` INT NOT NULL ,
`Specialization_idSpecialization` INT NOT NULL ,
`Name` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idStudent`) ,
CONSTRAINT `fk_Student_Specialization1`
FOREIGN KEY (`Specialization_idSpecialization` )
REFERENCES `mydb`.`Specialization` (`idSpecialization` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Student_Specialization1` ON `mydb`.`Student` (`Specialization_idSpecialization` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Discipline`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Discipline` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Discipline` (
`idDiscipline` INT NOT NULL ,
`Name` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idDiscipline`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Lecturer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Lecturer` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Lecturer` (
`idLecturer` INT NOT NULL ,
`Name` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idLecturer`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`GroupsYear`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`GroupsYear` ;
CREATE TABLE IF NOT EXISTS `mydb`.`GroupsYear` (
`idGroupsYear` INT NOT NULL ,
`Discipline_idDiscipline` INT NOT NULL ,
`Lecturer_idLecturer` INT NOT NULL ,
`Year` YEAR NOT NULL ,
PRIMARY KEY (`idGroupsYear`) ,
CONSTRAINT `fk_Groups_Year_Discipline1`
FOREIGN KEY (`Discipline_idDiscipline` )
REFERENCES `mydb`.`Discipline` (`idDiscipline` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_GroupsYear_Lecturer1`
FOREIGN KEY (`Lecturer_idLecturer` )
REFERENCES `mydb`.`Lecturer` (`idLecturer` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Groups_Year_Discipline1` ON `mydb`.`GroupsYear` (`Discipline_idDiscipline` ASC) ;
CREATE INDEX `fk_GroupsYear_Lecturer1` ON `mydb`.`GroupsYear` (`Lecturer_idLecturer` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Groups`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Groups` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Groups` (
`idGroups` INT NOT NULL ,
`GroupsYear_idGroupsYear` INT NOT NULL ,
`Lecturer_idLecturer` INT NOT NULL ,
PRIMARY KEY (`idGroups`) ,
CONSTRAINT `fk_Group_GroupsYear1`
FOREIGN KEY (`GroupsYear_idGroupsYear` )
REFERENCES `mydb`.`GroupsYear` (`idGroupsYear` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Group_Lecturer1`
FOREIGN KEY (`Lecturer_idLecturer` )
REFERENCES `mydb`.`Lecturer` (`idLecturer` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Group_GroupsYear1` ON `mydb`.`Groups` (`GroupsYear_idGroupsYear` ASC) ;
CREATE INDEX `fk_Group_Lecturer1` ON `mydb`.`Groups` (`Lecturer_idLecturer` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Current_Group_has_Student`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Current_Group_has_Student` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Current_Group_has_Student` (
`Groups_idGroups` INT NOT NULL ,
`Student_idStudent` INT NOT NULL ,
PRIMARY KEY (`Groups_idGroups`, `Student_idStudent`) ,
CONSTRAINT `fk_Groups_has_Student_Groups1`
FOREIGN KEY (`Groups_idGroups` )
REFERENCES `mydb`.`Groups` (`idGroups` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Groups_has_Student_Student1`
FOREIGN KEY (`Student_idStudent` )
REFERENCES `mydb`.`Student` (`idStudent` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Groups_has_Student_Student1` ON `mydb`.`Current_Group_has_Student` (`Student_idStudent` ASC) ;
CREATE INDEX `fk_Groups_has_Student_Groups1` ON `mydb`.`Current_Group_has_Student` (`Groups_idGroups` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`LongTimeAgo_Group_has_Student`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`LongTimeAgo_Group_has_Student` ;
CREATE TABLE IF NOT EXISTS `mydb`.`LongTimeAgo_Group_has_Student` (
`Groups_idGroups` INT NOT NULL ,
`Student_idStudent` INT NOT NULL ,
PRIMARY KEY (`Groups_idGroups`, `Student_idStudent`) ,
CONSTRAINT `fk_Groups_has_Student_Groups2`
FOREIGN KEY (`Groups_idGroups` )
REFERENCES `mydb`.`Groups` (`idGroups` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Groups_has_Student_Student2`
FOREIGN KEY (`Student_idStudent` )
REFERENCES `mydb`.`Student` (`idStudent` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Groups_has_Student_Student2` ON `mydb`.`LongTimeAgo_Group_has_Student` (`Student_idStudent` ASC) ;
CREATE INDEX `fk_Groups_has_Student_Groups2` ON `mydb`.`LongTimeAgo_Group_has_Student` (`Groups_idGroups` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Past_Group_has_Student`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Past_Group_has_Student` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Past_Group_has_Student` (
`Groups_idGroups` INT NOT NULL ,
`Student_idStudent` INT NOT NULL ,
PRIMARY KEY (`Groups_idGroups`, `Student_idStudent`) ,
CONSTRAINT `fk_Groups_has_Student_Groups3`
FOREIGN KEY (`Groups_idGroups` )
REFERENCES `mydb`.`Groups` (`idGroups` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Groups_has_Student_Student3`
FOREIGN KEY (`Student_idStudent` )
REFERENCES `mydb`.`Student` (`idStudent` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Groups_has_Student_Student3` ON `mydb`.`Past_Group_has_Student` (`Student_idStudent` ASC) ;
CREATE INDEX `fk_Groups_has_Student_Groups3` ON `mydb`.`Past_Group_has_Student` (`Groups_idGroups` ASC) ;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
For better understanding: http://i.stack.imgur.com/cGsjG.png
I hope someone will find a mistake in schema.
I've found the answer. Maybe not the best one, but thanks to this link http://aquaryus.wordpress.com/2011/04/11/tutorial-create-a-quick-jee-web-service-with-mysql-and-netbeans-glassfish/ I just replaced engine type InnoDB with MyISAM and everything now works fine! (at least it allows to create correct POJOs for JPA) But the question why InnoDB doesn't work is still open.
Related
I have two tables :
CREATE TABLE IF NOT EXISTS `DB`.`global_history` (
`ID` INT(11) AUTO_INCREMENT,
`ID_HISTORY` INT(11) NULL,
PRIMARY KEY (`ID`),
CONSTRAINT `FK_HISTORY_GLOBAL_HISTORY`
FOREIGN KEY (`ID_HISTORY`)
REFERENCES `DB`.`history` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
Second table :
CREATE TABLE IF NOT EXISTS `DB`.`history` (
`ID` INT(11) AUTO_INCREMENT,
`TIMESTAMP` DATETIME NOT NULL,
PRIMARY KEY (`ID`));
but when I try to delete a row in History (Second table) I get this error :
--> Cannot delete or update a parent row : a foreign key constraint fails
And I want the relationship to be #ManyToOne So when I remove a row from global_history it will not remove any row from history
And this is my model class :
Global history :
#ManyToOne
#JoinColumn(name = "ID_HISTORY", nullable = true)
private History history;
--> history is a simple class
when you define a F.K from a child (global_history) to parent (History table), child table can not has invalid F.K. so you should decide for deleting parent which cause F.K will be invalid.
a foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQL Server.
so if you want to prevent deleting corresponding child rows , you can set null value or default value by using following command:
ON DELETE SET NULL
ON DELETE SET DEFAULT
this is complete format:
CREATE TABLE child_table
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...
CONSTRAINT fk_name
FOREIGN KEY (child_col1, child_col2, ... child_col_n)
REFERENCES parent_table (parent_col1, parent_col2, ... parent_col_n)
ON DELETE CASCADE
[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
)
https://www.techonthenet.com/sql_server/foreign_keys/foreign_delete.php
I have a table of Orders, and a table of Order Status Updates that points to order.id. I need to get a list of Orders, but I need to join in order status updates because I don't want orders which last status is 'Cancelled'
CREATE TABLE `orders` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`end_customer_id` bigint(20) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `FK_end_customer` (`end_customer_id`),
CONSTRAINT `orders_ibfk_3` FOREIGN KEY (`end_customer_id`) REFERENCES `end_customers` (`id`) ON UPDATE CASCADE,
) ENGINE=InnoDB AUTO_INCREMENT=100333 DEFAULT CHARSET=utf8;
CREATE TABLE `order_status_updates` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`status` varchar(255) NOT NULL,
`date` datetime NOT NULL,
`order_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_order` (`order_id`),
CONSTRAINT `order_status_updates_ibfk_3` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
) ENGINE=InnoDB AUTO_INCREMENT=344180 DEFAULT CHARSET=utf8;
Current Criteria is:
final Criteria c = criteria("o")
.add(eq("o.endCustomer.id", endCustomerId));
return list(c.addOrder(desc("createdAt")));
I need to filter out Orders which latest status (sorted by Date) is Cancelled
Assuming a class called MyOrder and an ORDER_STATUS enum as well as id, status and createdAt field definitions:
final Criteria c = sessionFactory.getCurrentSession().createCriteria(MyOrder.class);
c.add(Restrictions.eq("id", yourId))
.add(Restrictions.ne("status", ORDER_STATUS.CANCELLED)
.addOrder(Order.desc("createdAt"));
I've got tables Artist, Concert, and Artist_Concert, which contains many-to many connections between Artist and Concert.
The problem is: after adding a Concert with few Artists, when trying to delete rows from Artist_Concert, it only deletes only one row and nothing happens when trying to delete any others.
This is how I'm trying to delete rows in Java:
stat = connect.createStatement();
res = stat.executeQuery ("SELECT idConcert FROM concerthall.concert where ConcertName = '"+conc+"';");
res.first();
int idconc = res.getInt(1);
stat.execute ("DELETE FROM concerthall.artist_concert WHERE idConc="+idconc+"");
Artist
CREATE TABLE IF NOT EXISTS `concerthall`.`Artist` (
`idArtist` INT NOT NULL AUTO_INCREMENT,
`ArtName` VARCHAR(45) NOT NULL,
`ArtFee` INT NULL,
PRIMARY KEY (`idArtist`))
ENGINE = InnoDB
Artist-Concert
CREATE TABLE IF NOT EXISTS `concerthall`.`Artist_Concert` (
`idCA` INT NOT NULL AUTO_INCREMENT,
`idArt` INT NOT NULL,
`IdConc` INT NOT NULL,
INDEX `idart_idx` (`idArt` ASC),
INDEX `idconc_idx` (`IdConc` ASC),
PRIMARY KEY (`idCA`),
CONSTRAINT `idart2`
FOREIGN KEY (`idArt`)
REFERENCES `concerthall`.`Artist` (`idArtist`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `idconct4`
FOREIGN KEY (`IdConc`)
REFERENCES `concerthall`.`Concert` (`idConcert`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
Concert
CREATE TABLE IF NOT EXISTS `concerthall`.`Concert` (
`idConcert` INT NOT NULL AUTO_INCREMENT,
`ConcertName` VARCHAR(45) NOT NULL,
`ConcertDateTime` DATETIME NOT NULL,
`Organizator` INT NOT NULL,
PRIMARY KEY (`idConcert`),
INDEX `concertorg_idx` (`Organizator` ASC),
CONSTRAINT `concertorg`
FOREIGN KEY (`Organizator`)
REFERENCES `concerthall`.`Organizator` (`idOrganizator`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
The easiest way to drop duplicates is:
ALTER IGNORE TABLE table ADD UNIQUE INDEX( a, b );
In the INDEX() part, enter the name(s) of the column(s) you only want unique entries for. I think you want:
ALTER IGNORE TABLE concerthall.artist_concert ADD UNIQUE INDEX( idConc );
Then drop the index.
I have a parent and child table.
schema: parent(int id,varchar desc), child(int id, varchar desc, int parent_id(foreignKey)).
From this I created entities and then JPAControllers from those entities using netbeans.
When I tried to store a parent with a collection of child it throws java.lang.IllegalArgumentException: An instance of a null PK has been incorrectly provided for this find operation.. After inspecting the JPA controllers generated by netbeans, I found out that they are assuming that the child objects in the parent have an ID and in my case since the child objects are also new I don't have an ID for them. But what I don't understand is that why the following code is present in the netbeans generated JPAController code.
Collection<Child> attachedChildCollection = new ArrayList<Child>();
for (Child childCollectionChildToAttach : parent.getChildCollection()) {
childCollectionChildToAttach = em.getReference(childCollectionChildToAttach.getClass(), childCollectionChildToAttach.getId());
attachedChildCollection.add(childCollectionChildToAttach);
}
parent.setChildCollection(attachedChildCollection);
Does anybody know any way to solve this other than manually editing JPAControllers.
Below code snippet shows how I tried to store parent.
Parent parentObj = new Parent();
parentObj.setDesc("parent");
Collection<Child> childCollection = new ArrayList<>();
Child childObj = new Child();
childObj.setDesc("child1");
childCollection.add(childObj);
Child childObj2 = new Child();
childCollection.add("childObj2");
parentObj.setChildCollection(childCollection);
parentJpaController.create(parentObj);
script for creating tables given below
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`PARENT`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`PARENT` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`DESC` VARCHAR(45) NULL ,
PRIMARY KEY (`ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`CHILD`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`CHILD` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`DESC` VARCHAR(45) NULL ,
`PARENT` INT NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `FK_CHILD_PARENT` (`PARENT` ASC) ,
CONSTRAINT `FK_CHILD_PARENT`
FOREIGN KEY (`PARENT` )
REFERENCES `mydb`.`PARENT` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
I am currently writing this manually using a line read from a file and am trying to read all table ddls where the table begins a_
An input of this:
Other stuff:
Other stuff:
create table a_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
stuff
create table a_table2 (
id number(10,0) not null,
primary key (id)
)
Other stuff:
create table b_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
other stuff
other stuff
should output only this
create table a_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
create table a_table2 (
id number(10,0) not null,
primary key (id)
)
Currently I am using LineReaders and remembering when I see create table and then reading everything until I see )
Is this the most efficient way? Is there some fancy reg ex I could use?
I tried the following reg ex but this didnt work as it just returns the whole string again. Perhaps the new lines are breaking it
"^.*create.*a_(.*?)\\).*$", "$1")
Any advice would be appreciated
Thanks
Try something like this:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copyLarge(getClass().getClassLoader().getResourceAsStream("input.txt"), baos);
String org = baos.toString();
final Pattern compile = Pattern.compile("(?s)(create table a_.*?\n\\)\n)");
final Matcher matcher = compile.matcher(org);
while (matcher.find()) {
System.out.println(matcher.group());
}
input.txt
Other stuff:
Other stuff:
create table a_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
stuff
create table a_table2 (
id number(10,0) not null,
primary key (id)
)
Other stuff:
create table b_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
other stuff
output
create table a_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
create table a_table2 (
id number(10,0) not null,
primary key (id)
)
Following regex based code will work as long as there is only 2 level nesting of parenthesis in the create table sql statements:
String sql = "Other stuff: \n\nOther stuff: \ncreate table a_table1 (\nid number(10,0) not null,\ntimestamp number(19,0) not null,\nprimary key (id)\n)\nstuff\ncreate table a_table2 (\nid number(10,0) not null,\nprimary key (id)\n)\n\nOther stuff: \ncreate table b_table1 (\nid number(10,0) not null,\ntimestamp number(19,0) not null,\nprimary key (id)\n)\nother stuff \n\nother stuff\n\n";
Pattern p = Pattern.compile(
"(?i)create\\s+table\\s+a_\\w+\\s+\\((?:[^()]+|\\([^()]+\\))*\\)"
);
Matcher m = p.matcher(sql);
while (m.find()) {
System.out.println(m.group());
}
OUTPUT
create table a_table1 (
id number(10,0) not null,
timestamp number(19,0) not null,
primary key (id)
)
create table a_table2 (
id number(10,0) not null,
primary key (id)
)