value of the foreign key as null in spring mvc hibernet using postgresql - spring

I am using spring mvc hibernate with postgresql.I have created two tables employee and accounts in which there is one to many mapping from employee to accounts,and the employee id is passed as the foreign key to accounts table, but i think the id is not generating. In the database table is generated of accounts table with the foriegn key as the employee id which is the primary key of the employee table, but with null value.
for table generation
#GeneratedValue(strategy = GenerationType.IDENTITY)

Related

Specify foreign key reference in Spring Data Rest, Update Request using HTTP PUT

I have an entity
#Entity
#Table(name="Indications")
#NamedQuery(name="Indication.findAll", query="SELECT i FROM Indication i")
public class Indication implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="IndicationID")
private int indicationID;
#Column(name="IndicationDescription")
private String indicationDescription;
#ManyToOne
#JoinColumn(name="ParentIndicationID")
private Indication indication;
}
As is evident in the entity the table has 3 columns
IndicationID which is a primary key
IndicationDescription which is a string
Indication which is a foreign key reference to a row in the same table, so it is many-to-one relationship
Since I am using Spring Data Rest I can create a new record using HTTP POST with the following body
{
"indicationDescription": "dummy1",
"indication": "/indications/1"
}
This creates a new row in the database with the content dummy1 and creates a foreign key reference to the record with id 1.
But when I try to update the foreign key reference using HTTP PUT with the following body
{
"indicationDescription": "dummy2",
"indication": "/indications/2"
}
While it updates the indicationDescription it does not actually update the foreign key reference.
I am aware that I can update the foreign key reference by using the URL http://localhost:8080/indications/7733/indication
and sending body as
http://localhost:8080/indications/2
after setting the content type to text/uri-list
My question is , Is there a way to perform the update in one request using the body similar to the one I used for creation of the record instead of making another update request using the URL - http://localhost:8080/indications/7733/indication
Thanks in advance.

Difference between MongoSpringData id and _id

I am using MongoDB community edition 4.2 on Windows 10 and inserting some documents. My requirement is to store some documents containing 'id' as field name while MongoDB is replacing it with '_id' and making it the primary key. Is it possible to disable this feature and store key '_id' as the separate autogenerated primary key column and store 'id' as well in separated column which is part of my document?
We are using Spring Data MongoDb Repository entities insertion into MongoDb. What we have observerd the entity having name 'id' as primary key get converted to _id while entities having other name like projectId or something stored as it is with another seperate column as _id generated by MongoDB as primary key. Suggestions are helpful.
Code:
#Autowired
TrackHistoryRepository trackHistoryRepository;
trackHistoryRepository.save(entity);
public interface TrackHistoryRepository extends MongoRepository<OdataEntity, String> {}
Entity:
public class CurrencyEntity implements OdataDeleteableEntity<Long> {
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
#Column(name="id")
private Long id;
}
MongoDB can't do that. _id in mongodb is auto-incremented and auto-generated id key for specific record or data field. And _id is primary key by default set by mongodb. You can also enter your user defined key(value) to the _id but you cant change name of field.

How to establish foreign key relationship with a non entity table in the database using Spring data JPA?

My spring boot project uses an existing database, I have a new model entity/table in my project that must have a foreign key constraint with an existing table in the database.
I've tried to find solution online but all the answers are for the case where both the tables are present as entities in that project and using some #ManyToOne, #OneToMany annotations.
I can't define those annotations because I don't have the reference table as an entity or model in my project.
Let's say I have class like:
#Entity(name = "user")
public class User {
#Id
#GeneratedValue
private long userId;
private long departmentId;
I want to put a foreign key contraint on the departmentId column to reference to id column of the existing department table that isn't defined as a model or entity in my project.
Thanks
Just do it as normal
example
#Column(name = "department_id")
private Department departmentId;
You can later access it Department.departmentId. Hope this helps.
Try it like this
#ManyToOne
#JoinColumn(name="(column name of current entity)", referencedColumnName="(column name in target entity)")
private Department departmentId;
you can skip the referencedColumnName if the column name is same in both the entities

Spring JPA one to many

I have two entities :
#Entity
#Table(name="Registration")
public class Registration{
#Id
private UUID uuid;
#OneToMany(cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE}, fetch = FetchType.LAZY)
#JoinColumn(name="registration", nullable = false)
private List<Payment> payment;
}
#Entity
#Table(name="Payment")
public class Payment {
#Id
private UUID uuid;
/*#ManyToOne(targetEntity = Registration.class) <-- MappingException: Repeated column in mapping for entity
private Registration registration;*/
}
This entities create two tables :
TABLE `registration` (
`uuid` binary(16) NOT NULL,
PRIMARY KEY (`uuid`))
TABLE `payment` (
`uuid` binary(16) NOT NULL,
`registration` binary(16) NOT NULL,
PRIMARY KEY (`uuid`),
CONSTRAINT `FK_jgemihcy9uethvoe3l7mx2bih` FOREIGN KEY (`registration`) REFERENCES `registration` (`uuid`))
I'm using Rest Service. I can access to
registration.payment
but not
payment.registration
why ? I need a relation oneToMany bidirectionnal ?
Yes, you need to add the payment.registration #ManyToOne relationship if you use it in your code.
Take into account that JPA allows you to map a SQL database model to an object oriented one. Once you have the mapping between your objects and your database, you always work at the object level. That's why, although you have the relationship in the database, your Payment object doesn't know anything about it unless you map it to an attribute.
Of course it applies when you are using you data model objects or performing JPQL or Criteria queries. If you use native queries you have access to the database model as it is.

Hibernate Mapping Annotation without Foreign Key

I have 2 tables ( Member, Department)
Member with MemberID, MemberName, DepartmentID
Department with DepartmentID, DepartmentID
Member is one to one to Department,
Department is one to many Member
Is there any ways to map them while creating the model class without using foreign key constraint in database?

Resources