I am using Angularjs, Spring jpa repository, hibernate.
The problem is "Unable to save the id of the Customer in the order table."
table names: Order, CustomerGlobal
reference col name for Customer in Order table is customerGlobal_id.
Order belongs to a Customer. Order table has a customerGlobal_id field.
In the Entity (Order), I have defined
#ManyToOne(optional = false) #JsonIgnore
CustomerGlobal customerGlobal;
Order belongs to one Customer. If I specify a JoinColumn, hibernate is generating the column in the database. So, I think there is no issue there.
I have added Getter and Setter for the customerGlobal field.
I am using JPA repository, the interface is defined as follows:
public interface OrderRepo extends JpaRepository<Order, Long>, JpaSpecificationExecutor<Order> {
...
}
I am assuming that this would work fine too.
Here is the relevant part of the html:
<select ng-options="convertToInt(customer.id) as customer.name for customer in c" class="form-control " name="customerGlobal" ng-model="req.customerGlobal" required></select>
This is part of a form which gets saved from the associated controller. The values for c are obtained by get request on load.
[{"id":1,"name":"XYZ","contactPerson":"ABC","mobile":"1111111111","email":"abc#adef.com",}]
This seems to be working fine. I get a dropdown list of the customers in the table.
When I save the form, customerGlobal_id and other field values are sent via POST but the customerGlobal_id does not get saved to the database.
This does not seem to be a very specific problem. This is a basic many-to-one relationship that is not getting saved. I am not completely familiar with Angularjs. So please help me out with this. Thanks in advance.
In order to save a relationship with spring rest, you shouldn't use the id of the entity, but the reference url of the entity. So let's say that you have a Car entity that has an association with a Make entity, and you want to set the make of the car. Here is what you should do:
Step 1. get the Make entity:
-get a specific `Make`: localhost:8080/makes/1
-get a a list of `Make`: localhost:8080/makes
and select the one you want:
{
"name" : "Toyota",
"country" : "Japan",
"_links" : {
"self" : {
"href" : "http://localhost:8080/makes/1"
}
}
Step 2. Now insert / update a Car like this:
POST/PUT http://localhost:8080/cars/123
{
"model" : "Corolla",
"year" : "2006",
"hp" : 95,
"make" : "http://localhost:8080/makes/1"
}
and NOT like this:
{
"model" : "Corolla",
"year" : "2006",
"hp" : 95,
"make" : 1
}
Spring Data Rest understands the reference url of the entity.
Related
rI am new to laravel 7 and I still have a few small difficulties.
I have two tables: users and services. These two tables have a relation to retrieve a user's service.
$users= User::with('poseur')->get();
It returns all users even those who do not meet the conditions of my relationship.
I use scope in service model:
public function scopePoseurs(){
return $query->whereRaw('slug','pos')
}
And i use belongsTo relation in user model :
public function poseur(){
return $this->belongsTo('App\Service')->poseurs();
}
Exemple: we hase 2 users:
first: Daniel have service slug = 'pos',
second: Patrick have service slug ='dev'
When i use $users=User::with('poseur')->get();, i see Daniel and Patrick.
While there should be only Daniel.
Can you help me understand ?
Thanks !
with() is for eager loading. That basically means, along the main model, Laravel will preload the relationship(s) you specify. This is especially helpful if you have a collection of models.
If you want to return all user, that has relation with poseur only, then use has() method :
$users= User::has('poseur')->get();
Ther is also a method called whereHas(), which allows you to specify additional filters for the related model to check :
$users = User::whereHas('poseur', function($q){
$q->where('created_at', '>=', '2020-01-01 00:00:00');
})->get();
I have a table category it has a self relation to have belongs_to simply to be able to create subcategories and assign them to previously created categories and now i need to be able to either assign the new category to another (super) category or to assign it to none.
my model is :
public $belongsTo = [
'category' => ['plugin\pdf\models\Category']
];
This is found in the documents under backend -> forms. Look for the relation under widget fields. The option you is called emptyOption and it will be added in your fields.yaml file.
You would add the emptyOption like so to model's fields.yaml.
field:
label: Field Label
nameFrom: name
descriptionFrom: description
span: auto
type: relation
emptyOption: None
I have an app with the following DB structure currently, it uses Firebase Database and is on Swift IOS:
"Posts" : {
"Dm8iyaXXdTOJGsymEiLNVO6OdDK2" : {
"post:570915537" : {
"Media" : {
"image" : {
"mediaUrl" : "https://firebaseURL",
"postTimeStamp" : 5.70915539085856E8,
"timeStamp" : 5.7091551482329E8
}, ...
I was now going to add followers to it. I was thinking that I would either add a whole new group:
"Followers" : {
"Dm8iyaXXdTOJGsymEiLNVO6OdDK2" : {
"Following" : {
follower1: "Dm8iyaXXdTOJGsymEiLNVO6OdDK2";
//other followers
}, ...
Or add a new node to the original group and add them there. The last option is to do something similar to what is done above but in the "Users" section.
What is the best course of action?
A follow/unfollow schema using Firestore could be this one:
2 roots collections, one that holds users and another one that holds the following relationship between users through a composite key.
users/{userID}
.. userData
.. followerCount
.. followedCount
following/{followerID_followedID}
.. followerId
.. followedId
.. createdAt
When a userA starts to follow a userB:
set a new document with the id userAuid_userBuid in the
following collection.
triggers a Cloud Function that will run a transaction to update the
counters of both users.
Do the reverse thing when a user stops to follow another user.
In the client, you can know if userA follows userB simply by checking if the document userAuid_userBuid exists inside the following collection.
You can also get the list of a user's followers by querying the collection where the followedId == the current user Id.
Hope that helps you.
I am using Backand BaaS for my ionic app. My model consists of two tables:
1. Users
2. Trips
A one-to-many relationship has been established between 'Users' and 'Trips'.
'Users' has a collection field that is a collection of 'Trips' and 'Trips' has a owner field that is an object of 'Users'.
What is the correct way to create an object so that upon creating I am assigning the correct owner ('Users' object) to 'Trips' collection field ?
When POSTing a new Trip use this for the Trip Object
{
tripName:"asdf"
owner:1
}
In the users object it will look like this
{
id:1
fName:"david"
trips:[{__metadata: {id: 1}, tripName: "asdf"}]
}
I want to implement some kind of tagging functionality to my app. I want to do something like...
class Item(db.Model):
name = db.StringProperty()
tags = db.ListProperty(str)
Suppose I get a search that have 2 or more tags. Eg. "restaurant" and "mexican".
Now, I want to get Items that have ALL, in this case 2, given tags.
How do I do that? Or is there a better way to implement what I want?
I believe you want tags to be stored as 'db.ListProperty(db.Category)' and then query them with something like:
return db.Query(Item)\
.filter('tags = ', expected_tag1)\
.filter('tags = ', expected_tag2)\
.order('name')\
.fetch(256)
(Unfortunately I can't find any good documentation for the db.Category type. So I cannot definitively say this is the right way to go.) Also note, that in order to create a db.Category you need to use:
new_item.tags.append(db.Category(unicode(new_tag_text)))
use db.ListProperty(db.Key) instead,which stores a list of entity's keys.
models:
class Profile(db.Model):
data_list=db.ListProperty(db.Key)
class Data(db.Model):
name=db.StringProperty()
views:
prof=Profile()
data=Data.gql("")#The Data entities you want to fetch
for data in data:
prof.data_list.append(data)
/// Here data_list stores the keys of Data entity
Data.get(prof.data_list) will get all the Data entities whose key are in the data_list attribute