When I click submit in localhost:8080/myregister.jsp(controller:
localhost:8080/user/register),it gets:
Request processing failed; nested exception is
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
com.cn.hnust.dao.UserMapper.insert
When I click submit in localhost:8080/login.jsp,(controller: localhost:8080/user/login),it works.
[MYSQL:
DROP TABLE IF EXISTS user_t;
CREATE TABLE user_t (
username varchar(40) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
]
I use mybatis-generator-core-1.3.2 to general UserMapper.xml,but why only the 'insert' function can't run?
The whole Maven project is in github:https://github.com/Bonsen/Spring-SpringMVC-mybatis.
Related
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)
);
CREATE TABLE `role_details` (
`role_id` int(11) NOT NULL AUTO_INCREMENT,
`role_name` varchar(45) DEFAULT NULL,
`role_desc` varchar(100) DEFAULT NULL,
`rights` varchar(300) DEFAULT NULL,
PRIMARY KEY (`role_id`)
)
CREATE TABLE `user_details` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`display_name` varchar(45) DEFAULT NULL,
`password` varchar(45) DEFAULT NULL,
`emp_id` int(11) DEFAULT NULL,
`role` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`mobile` varchar(45) DEFAULT NULL,
`creation_time` datetime DEFAULT CURRENT_TIMESTAMP,
`status` varchar(45) DEFAULT NULL,
PRIMARY KEY (`user_id`)
)
This is my database schema. I am using spring security.
But I am confused...what I need to write in spring-security.xml ?
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query=
"SELECT * FROM hmis_db.user_details where display_name=? and status='active'"
authorities-by-username-query=
"**QUESTION** " />
</authentication-provider>
</authentication-manager>
Actually, I want to create Role based dashboard. and 'rights' in role_details table that specifies the access menu list..and according it it will generate role based dashboard.
Actually I can't see the relationship between your UserDetails table and the RoleDetails one, maybe I'm losing something.
When I have made such a kind of entities schemas, I allways made a 1-to-n relations between Users and roles, so one user can have one role or more.
But assumming you are conguring a 1-1 relation from user to role, and assuming the ROLE_xxx you are looking for is on the role field in UserDetails table, your authorities-by-username-query must be more or less like this:
"select display_name as username, role as authority from user_details where display_name =? "
If the field role in user_details is the foreign key which must match the field role_name in role_details, and rigths are actually the authorities which are referenced in access elements of security_intercept urls, it should be this way:
"select u.display_name as username, r.rights as authority
from user_details as u INNER JOIN role_details as r ON u.role = r.role_name
where u.display_name =? "
But here you might have a trouble if rights are a comma separated list of authorities. authorities-by-username-query expects to receive a list of rows with an authority in each of this rows. If this is what you were seeking, you should consider changing the schema or making even a view of the tables which returns a right per row result
EDIT: I should do this way:
This is the creation script:
CREATE TABLE IF NOT EXISTS `role_details` (
`role_id` INT(11) NOT NULL AUTO_INCREMENT,
`role_name` VARCHAR(45) NULL DEFAULT NULL,
`role_desc` VARCHAR(100) NULL DEFAULT NULL,
`role_authority` VARCHAR(300) NOT NULL,
PRIMARY KEY (`role_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `user_details` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT,
`display_name` VARCHAR(45) NULL DEFAULT NULL,
`password` VARCHAR(45) NULL DEFAULT NULL,
`emp_id` INT(11) NULL DEFAULT NULL,
`email` VARCHAR(45) NULL DEFAULT NULL,
`mobile` VARCHAR(45) NULL DEFAULT NULL,
`creation_time` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
`status` TINYINT(1) NULL DEFAULT 0,
PRIMARY KEY (`user_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `user_role_details` (
`user_details_user_id` INT(11) NOT NULL,
`role_details_role_id` INT(11) NOT NULL,
PRIMARY KEY (`user_details_user_id`, `role_details_role_id`),
INDEX `fk_user_details_has_role_details_role_details1_idx` (`role_details_role_id` ASC),
INDEX `fk_user_details_has_role_details_user_details_idx` (`user_details_user_id` ASC),
CONSTRAINT `fk_user_details_has_role_details_user_details`
FOREIGN KEY (`user_details_user_id`)
REFERENCES `user_details` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_user_details_has_role_details_role_details1`
FOREIGN KEY (`role_details_role_id`)
REFERENCES `role_details` (`role_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
Note I deleted column 'role' in user_details, renamed 'rights' to 'role_authority' in role_details and changed 'status' from varchar to tinyint(1) to use it as boolean.
Then, the user details sqls:
users-by-username-query=
"SELECT display_name as username, password, status as enabled
FROM user_details as u WHERE u.display_name = ? and status = 1;"
authorities-by-username-query=
"Select u.display_name as username, r.role_authority as authority
FROM
user_details as u
INNER JOIN user_role_details as urd ON u.user_id = urd.user_details_user_id
INNER JOIN role_details as r ON urd.role_details_role_id = r.role_id
WHERE u.display_name = ?"
This way you can have multiple roles binded to each user
Refer to following link that covers basics and example http://en.tekstenuitleg.net/blog/spring-security-with-roles-and-rights
Once you familiarize yourself with basics, please look deeper into Spring Security documentation https://docs.spring.io/spring-security/site/docs/3.0.x/reference/ns-config.html before you implement that code for production.
If you are using Spring Security 4, you may prefer annotations based configurations like
#Configuration
#EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
#Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("userpwd").roles("USER");
auth.inMemoryAuthentication().withUser("admin").password("adminpwd").roles("ADMIN");
}
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.antMatchers("/admin/**").access("hasRole('ADMIN')")
.and().formLogin()
.and().exceptionHandling().accessDeniedPage("/Access_Denied");
}
}
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?
As per this link, I'm trying to implement the Persistent Token Approach for the "remember me" functionality. The tokens are stored in my database (postgres). I have the datasource set up properly, however, when I try doing:
<remember-me data-source-ref="dataSource" />
My database does not get populated with the token data automatically. Otherwise, the "remember me" functionality works well (only the database doesn't get populated automatically). Any ideas why?
Table ddl:
create table users(
uid serial primary key,
username varchar(255) not null,
password varchar(255) not null,
firstname varchar(255) not null,
lastname varchar(255) not null,
enabled boolean not null,
constraint cs_username_unq unique(username),
constraint cs_uid_username_unq unique(uid, username)
);
create table authorities (
username varchar(255) not null,
authority varchar(255) not null,
constraint fk_authorities_users foreign key(username) references users(username)
);
create unique index ix_auth_username on authorities (username,authority);
create table persistent_logins (
username varchar(255) not null,
series varchar(255) primary key,
token varchar(255) not null,
last_used timestamp not null
);
I'm new to Joomla! and also new to the component development.
I want to create a component which be able to link between 2 tables.
joomla v 1.6:
A table's structure:
CREATE TABLE `#__a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
B table's structure:
CREATE TABLE `#__b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
AB table's structure:
CREATE TABLE `#__ab` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_a` int(11) NOT NULL,
`id_b` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
Assume that we have already created the basic create, edit page and delete action with the MVC for A and B (created from the hello world component). From the hellow world tutorial we could know clear about the file and folder structure and the componet's code, but in the tutorial there is only 1 table, but here there is 2 tables and also new id id table.
So it means that in the component we're developing here, there are 2 submenus.
In the edit or new page
There is A details block which we can fill name.(done from the tutorial)
There is B linking block which we can choose the B (select option, can select more then 1) to add into the A
and also display list the B we added. In every item in the list, it has a delete button or link to unlink between the A and B.
Any ideas how to do please?
Thanks and Best Regards Friends,
Rithy
First you need some logic that will save the results in the reference table and the second part is to retrieve the data to be shown on the user side. You need a Model:
class CompControllerA extends JControllerForm {
// Here put your save code
//....
function save() {
$formData = JRequest::getVar('jform');
$bRecords = $formData['bRecords'];
$aRecordId = $formData['id'];
$referenceModel->delete($aRecordId); // Delete all records that has same ID as current record
foreach($bRecords as $row) {
$data['id']=0;
$data['a_id']=$aRecordId;
$data['b_id']=(int)$row;
$bModel->save($data);
}
// dont forget to call parent method
parent::save();
}
}
Next step is when you create the for just take the results from the reference table and show the correct results in the form.
Here is some example code of a component I've built.
class IbookModelExtra extends JModelAdmin
{
protected function loadFormData()
{
$db =& $this->getDbo();
$query = $db->getQuery(true);
$query->select('b_id')->from('#__table_a_b')->where('a_id='.$data->id);
$db->setQuery((string)$query);
$data->b = $db->loadResultArray();
}
}