Here are some entities:
public class Zone : AbstractGuidKeyedEntity
{
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
public class ZoneGroup : AbstractGuidKeyedEntity
{
public virtual string Name { get; set; }
public virtual ICollection<Zone> Zones { get; set; }
}
public class Contract: AbstractGuidKeyedEntity
{
public virtual string Name { get; set; }
public virtual ICollection<Zone> Zones { get; set; }
}
Here are the rules:
A Zone can belong to a ZoneGroup or a Contract.
A Zone cannot belong to both a ZoneGroup and a Contract.
Here is what I want:
One join table that has a unique id on zone.
What I will settle for:
Something that works.
How do I fluently map this with Entity Framework 4.1?
Thank you for your brain power.
-- EDIT --
Here are the tables I would like.
Zone
--------------------------
| ZoneId | Name |
==========================
| lkj-lakjd | Front |
| asd-jkllk | Rear |
--------------------------
ZoneGroup
--------------------------
| ZoneId | Name |
==========================
| uio-asdfd | Z71 Boat |
--------------------------
Contract
--------------------------
| ZoneId | Name |
==========================
| zxc-qwert | Hugo Taylor|
--------------------------
ZoneGroupContractZone
--------------------------------------
| ZoneId | ZonableId | Type |
======================================
| lkj-lakjd | uio-asdfd | ZoneGroup |
| asd-jkllk | zxc-qwert | Contract |
--------------------------------------
Related
I'm trying to make a todo application on Spring. It's almost finished but I got a problem. I need to filter todos by user id and select them which is in the between given dates. I've created a method for this in my TodoRepository. But when I send the request to the url, it gives me empty array.
Here's the repository class:
All methods work fine except the one I mentioned btw.
#Repository
public interface TodoRepository extends JpaRepository<Todo, Long> {
List<Todo> findTodosByUserId(Long id);
List<Todo> findTodosByUserIdOrderByDateAsc(String id);
List<Todo> findAllByDateBetween(Date date, Date date2);
List<Todo> findTodosByDateBetweenAndUserId(Date date, Date date2, Long id);
}
That's the method i use in the TodoService class:
#Service
public class TodosService {
#Autowired
TodoRepository todoRepository;
//other methods..
public List<Todo> filterTodosByUserId(FilterTodoByUserDto dto){
Date date1 = dto.getDate1();
Date date2 = dto.getDate2();
Long id = dto.getId();
return todoRepository.findTodosByDateBetweenAndUserId(date1, date2, id);
}
}
FilterTodoByUserDto class:
public class FilterTodoByUserDto {
private Long id;
private Date date1;
private Date date2;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getDate1() {
return date1;
}
public void setDate1(Date date1) {
this.date1 = date1;
}
public Date getDate2() {
return date2;
}
public void setDate2(Date date2) {
this.date2 = date2;
}
#Override
public String toString() {
return "FilterTodoByUserDto{" +
"id=" + id +
", date1=" + date1 +
", date2=" + date2 +
'}';
}
}
And lastly, controller class:
#RestController
#RequestMapping("/api")
public class TodoController {
#Autowired
private TodosService todosService;
//other methods..
#GetMapping("user/todos/filter")
public List<Todo> filterTodosById(#RequestBody FilterTodoByUserDto dto){
List<Todo> todos = todosService.filterTodosByUserId(dto);
return todos;
}
//other methods..
}
Request body that i sent with postman:
{"userId":"1", "date1":"2021-01-01", "date2":"2000-01-01"}
Database output:
mysql> select * from todos;
+----+----------------------------+-------------+-------------+---------+
| id | date | description | todo_status | user_id |
+----+----------------------------+-------------+-------------+---------+
| 1 | 2012-12-12 00:00:00.000000 | deneme | TODO | 2 |
| 2 | 2010-10-10 00:00:00.000000 | deneme2 | TODO | 2 |
| 3 | 2010-01-01 00:00:00.000000 | deneme5 | DONE | 1 |
| 4 | 2010-01-01 00:00:00.000000 | deneme | DONE | 1 |
| 5 | 2010-01-01 00:00:00.000000 | deneme | DONE | 1 |
| 6 | 2010-01-01 00:00:00.000000 | deneme | DONE | 1 |
| 7 | 2010-01-01 00:00:00.000000 | deneme | DONE | 1 |
| 8 | 2010-01-01 00:00:00.000000 | deneme | DONE | 1 |
| 9 | 2010-01-01 00:00:00.000000 | deneme | DONE | 2 |
+----+----------------------------+-------------+-------------+---------+
9 rows in set (0,01 sec)
Change your request json like this:
{
"id": "1",
"date1": "2000-01-01",
"date2": "2021-01-01"
}
Your fields in your DTO model should match your json fields. (userId -> id)
BETWEEN returns true if the value of date is greater than or equal to the value of begin_expression and less than or equal to the value of end_expression. (.. date BETWEEN date1 AND date2)
You are getting empty array due to your postman request is logically wrong. When BETWEEN use date1 must be the minimum value and date2 must be the maximum value.
ex: The following date example uses the BETWEEN condition to retrieve values within a date range.
SELECT *
FROM todos
where user_id=1
and date between '2000-01-01' and '2021-01-01';
Above BETWEEN SQL would be equivalent to the following SELECT statement:
SELECT *
FROM todos
where user_id=1
and date >= '2000-01-01' and date<= '2021-01-01';
I am trying to post using JpaRepository and spring boot and mysql
I have a table that looks like this
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | varchar(255) | NO | PRI | NULL | |
| user | int(11) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
Whenever i try to save data the column user is always 0 no matter what value i pass.
My Model class looks like this
#Entity
#Table(name = "cart")
public class Cart {
#Id
#Column(name = "id", unique = true, nullable = false)
private String id;
#Column(name = "user")
private Integer user;
// getters and setters
}
The repository looks like this
#Repository
public interface CartRepository extends JpaRepository<Cart, String> {
}
The controller looks like this
#RequestMapping("/api/v1/cart")
#RestController
public class CartController {
#Autowired
CartRepository cartRepository;
#PostMapping(value = "/postCart")
public String postCart(#RequestBody Cart cart){
cartRepository.save(cart);
return "Success";
}
}
Something is strange. Your id field is a String, yet in the DB its a int(11). It should be a variation of varchar. Regarding that user change it's type from int to Integer. A int cannot be null so it probably gives it the value 0.
Try also to recreate your DB model so that you are 100% sure it's matching your entity model.
I am developing a ASP.NET Core frontend to connect to my Google Cloud SQL MySQL database.
I have the following model:
namespace shipping.Models
{
public class ShippingAppContext : DbContext
{
public ShippingAppContext(DbContextOptions<ShippingAppContext> options) : base(options) { }
public DbSet<Ship> ships { get; set; }
public DbSet<Company> companies { get; set; }
}
public class Ship
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Company Company { get; set; }
}
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Ship> Ships { get; set; }
}
}
and I iterate over _context.ships.ToListAsync() in a razor page which shows ship.Name and ship.Company.Name.
and my database is as follows:
MySQL [shipping]> show tables;
+--------------------+
| Tables_in_shipping |
+--------------------+
| companies |
| ships |
+--------------------+
MySQL [shipping]> select * from ships;
+------+-----------------+---------+
| Id | Name | Company |
+------+-----------------+---------+
| 1 | Eugen Maersk | 1 |
| 2 | Madrid Maersk | 1 |
| 3 | COSCO Vancouver | 4 |
| 4 | COSCO Rotterdam | 4 |
+------+-----------------+---------+
MySQL [shipping]> select * from companies;
+------+----------------------------------------------+
| Id | Name |
+------+----------------------------------------------+
| 1 | A.P. Moller–Maersk Group |
| 2 | Mediterranean Shipping Company S.A. (MSC) |
| 3 | CMA CGM Group |
| 4 | China Ocean Shipping (Group) Company (COSCO) |
+------+----------------------------------------------+
This works fine with just show ship.Name but as soon as I try to show ship.Company.Name I get: MySqlException: Unknown column 's.CompanyId' in 'field list'.
I have done a search and CompanyId is not referenced anywhere in the project. The ships table does not contain a CompanyId column.
I guess this is probably because I have created the tables and data directly in the sql instance rather than using dotnet ef to create the database and upload it. But I don't think this is possible on Google Cloud SQL?
How can I make efcore do INNER JOIN from my model?
If you are manually create the database , you can manually add the table schema with FOREIGN KEY constraints relationship(FK_ships_companies_CompanyId) ,so that you could query the related data like :
var result = _context.ships.Include(a=> a.Company).ToList();
I asked a similar question in "Database Administrator" but sadly no one answered the question so I decided to transfer the data.
I have a table like this:
------------------------------------------------------
Parts | Owner | Number | Item_ID | ...
------------------------------------------------------
PartB | Adam | 4 | Item_a,Item_b,Item_z,...
ConD | Steve | 2 | Item_b,Item_c,Item_g,...
I wanted to have each value as a separate row:
------------------------------------------------------
Parts | Owner | Number | Item_ID | ...
------------------------------------------------------
PartB | Adam | 4 | Item_a
PartB | Adam | 4 | Item_b
PartB | Adam | 4 | Item_z
ConD | Steve | 2 | Item_b
ConD | Steve | 2 | Item_c
ConD | Steve | 2 | Item_g
Based on this answer , I tried :
SELECT Parts,
Owner,
Split.a.value('.', 'VARCHAR(100)') Item_ID
FROM (SELECT Parts,
Owner,
Cast ('<M>' + Replace(Item_ID, ',', '</M><M>') + '</M>' AS XML) AS Data
FROM Table_1) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
But I get an error:
You have an error in your SQL syntax...near '('.', 'VARCHAR(100)')...
Can be done easly with a 3rd party language like C# since you only need to run it once.
And even easier with a light ORM like Dapper in this example:
[Table("TableA")]
class table {
[Key]
int id {get; set;}
string Parts {get; set;}
string Owner {get; set;}
int Number {get; set;}
string Item_ID {get; set;}
...
}
[Table("TableB")]
class newtable {
[Key]
int id {get; set;}
string Parts {get; set;}
string Owner {get; set;}
int Number {get; set;}
string Item_ID {get; set;}
...
}
Func<SqlConnection> conn = () => new SqlConnection("Data Source=127.0.0.1; Initial Catalog=;User Id=root;Password=***");
var newdata = new List<newtable>();
foreach (var d in conn().Query<table>("Select * FROM table"))
{
foreach (var cd in d.Item_ID.Split(','))
{
newdata.Add(new newtable() { Parts = d.Parts, Owner = d.Owner, Number = d.Number, Item_ID = cd.Trim(), ... });
}
}
//insert the new datas
foreach (var data in newdata) {
conn().Insert(data);
}
I need to retrieve all the account numbers of the transaction table/object which given below. Using mysql for database.
Entity Class
public class Transaction implements java.io.Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "cr_account_ref")
private String crAccountRef;
#Column(name = "dr_account_ref")
private String drAccountRef;
#Column(name = "amount")
private String amount;
// getters and setters.
}
My Table
| cr_account_ref | dr_account_ref | amount |
|----------------------------------------------------------
|1000001 |2000009 | 100 |
|1000002 |2000008 | 500 |
|1000001 |2000006 | 100 |
|1000003 |2000004 | 500 |
|1000005 |2000008 | 611 |
|1000001 |2000009 | 300 |
|1000002 |2000004 | 120 |
-----------------------------------------------------------
What i need is, distinct of all the account numbers(both debit and credit.).
| cr_account_ref |
|----------------------
|1000001 |
|1000002 |
|1000003 |
|1000005 |
|2000004 |
|2000006 |
|2000008 |
|2000009 |
-----------------------
This is easy with pure jdbc, but i need to do the this with HQL.