JAVERS: how to set a custom property as "local_ID" - java

Hello i have an entity that have an inner id autogenerated by db and the uuid used as the actual id in the rest of the project. Immagine something like this:
#Entity
#Data
public class Task {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(nullable = false)
private Long innerId;
#Column(nullable = false, unique = true)
#Type(type="uuid-char")
private UUID uuid;
private String name;
}
i would like to set the uuid as the ID for javers, is there any way of doing so ? becaus javers "detects" the "persistence id annotation" and establish that as its own id.
I tried adding to the uuid column the annotation
#org.javers.core.metamodel.annotation.Id
but in this case javers creates a composite id made of innerId and uuid, and this doesn't works for me

Related

SQL JPA Hibernate refer user to parent user

I want to create an API with users where I can make referals to other user and store the users that refearal me.
#Entity
#Table(name="requirement")
#Data
#NoArgsConstructor
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id", nullable = false)
private Long id;
#Column(name = "name")
private String name;
private User parentUser;
}
CREATE TABLE user(
id serial PRIMARY KEY,
name VARCHAR,
user_id INT,
what is the best way to handle this case
I want to create an API with users where I can make referals to other user and store the users that refearal me.
You can use #OneToOne Relation between parent entity if you have a related column like 'parent_user_id'.
#OneToOne
#JoinColumn(name = "parent_user_id")
private User parentUser;

Same ID from sequence for 2 tables - JPA

I have 2 entities that use the same sequence as the primary key, how do I map?
Example:
#Entity
#Table("employeT")
public class Employe(){
#SequenceGenerator(name = "generator_id", sequenceName = "seq_id")
#GeneratedValue(generator = "generator_id")
#colunm(name = "id")
private Integer id;
#colunm(name = "nameEmp")
private String name;
#JoinColumn(name = "id")
private Computer computer;
}
#Entity
#Table("computerT")
public class Computer(){
#SequenceGenerator(name = "generator_id", sequenceName = "seq_id")
#GeneratedValue(generator = "generator_id")
#colunm(name = "id")
private Integer id;
#colunm(name="name_computer")
private String nameComputer;
}
I need save employe and computer with same id, generated by Employe save.
There are three things to do with your code to work the way to want to.
Add #OneToOne annotation to indicate that Employee and Computer are in relation.
Delete information about #SequenceGenerator from your Computer entity and add #Id annotation
Add #MapsId annotation. [More info]
So it would look something like this :
#Entity
#Table("employeT")
public class Employe(){
#Id
private Integer id;
#Colunm(name = "nameEmp")
private String name;
#OneToOne
#JoinColumn(name = "computer_id")
#MapsId
private Computer computer;
}
Why?
#OneToOne annotation indicates relation between entities.
#SequenceGenerator is redudant since we "copy" id from Computer entity.
#Id annotation is mandatory to indicate that this field is our primary key.
Last but not least, #MapsId annotation do the magic, where it 'borrows' id from relation.
More info in the link I attached earlier.

Hibernate String Primary Key with Annotation

I am trying to create a Privilege class with Annotations whose Primary Key is a String. I will assign them manually while inserting. Therefore no need for hibernate to generate a value for it. I'm trying to do something like that:
#Id
#GeneratedValue(generator = "assigned")
#Column(name = "ROLE_NAME", nullable = false)
private String roleName;
But it throws that exception:
Caused by: org.hibernate.AnnotationException: Unknown Id.generator: assigned
How can I configure a String primary key with annotations?
Since the roleName is not auto-generated, you should simply not annotate it with #GeneratedValue:
#Id
#Column(name = "ROLE_NAME", nullable = false)
private String roleName;
#Id
#Column(name = "USER_ID",unique=true,columnDefinition="VARCHAR(64)")
private String userId;
This worked for me by representing the columnDefinition type with column annotation during the save or update.
Just use the #Id annotation which lets you define which property is the identifier of your entity. You don't need to use the #GeneratedValue annotation because I don't think you want hibernate to generate this property for you.
Even in the XML configuration based approach its an optional tag and can be skipped.
you can enter this way, suppose position is primary key of that Salary entity,
#Id
#Column (name = "POSITION", nullable = false)
private String position;
then, it will work

How to generate ID for entity from the existing ORACLE sequence?

I have a Table (for a long time ago), call it TABLE_A, and I have an entity class for this Table:
#Entity
#Table(name = "TABLE_A")
public class TableA implements Serializable {
#Id
#Basic(optional = false)
#Column(name = "ID")
//what else should I write here, to get the value from the existing sequence (seq_table_a_id) from database?
private Long id;
#Basic(optional = false)
#Column(name = "VALID_TO_DT")
private String name;
getters/setters...
}
I had created a sequence for this table in ORACLE a long time ago, and I want to give values for the new item's ID from this sequence. How should I write this code in java entity with annotations? If you could write an example for my code, that would be helpful!
And should I write anything else maybe in the persistance.xml?
The name of the existing sequence is: seq_table_a_id
You should check the annotation #GeneratedValue and #SequenceGenerator
#Id
#GeneratedValue(generator="seqGen")
#SequenceGenerator(name="seqGen",sequenceName="seq_table_a_id", allocationSize=1)
private Long id;
Check this link

ORACLE: org.hibernate.ObjectNotFoundException: No row with the given identifier exists

I am using Hibernate(3.0) + Oracle DB (10g) for my application.
My domain object are like PluginConfig.java
#Entity
#Table(name = "plugin_config" , schema = "test")
#org.hibernate.annotations.Cache(usage =org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
public class Config {
private static final long serialVersionUID = -1959019321092627830L;
/** This object's id */
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
protected long id;
#OneToOne
#JoinColumn(name = "plugin_id")
private Plugin plugin;
#Column(name = "config_name")
#NaturalId(mutable = true)
private String name;
#Column(name = "config_desc")
private String description;
another domain object Plugin.java
#Entity
#Table(name = "plugin", schema = "test")
#org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
public class Plugin implements Serializable {
private static final long serialVersionUID = 5694761325202724778L;
/** This object's id */
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
protected long id;
#Column(name = "plugin_name")
#NaturalId
private String pluginName;
#OneToOne
#JoinColumn(name = "plugin_config_id")
private PluginConfig pluginConfig;
#Column(name = "plugin_desc")
private String description;
Whenever i try to load Plugin via my database service method(using #Transactional annotation and hence it loads all its children as well) i get the following error
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [PluginConfig#53]
There is actually no row in plugin_config table with id = 53.
And also no plugin table entry has plugin_config_id=53.
So from where is hibernate picking these values ?
I noticed one thing here, the value "53" is actually the row number from the plugin_config table which should have been reffered.
See the below image:
This is the plugin config that my query should be looking for. But it tries to search it with identifier #53(Row no:) instead of the #95(value of the primary key "id").
Where can i be going wrong with this ?
I don't know if this is teh best solution, but you can use #NotFound annotation with IGNORE command to let Hibernate discard exception adn set pluginConfig to null.
#OneToOne
#JoinColumn(name = "plugin_config_id")
#NotFound(action = NotFoundAction.IGNORE)
private PluginConfig pluginConfig;

Categories