How can I tell hibernate to ignore a field during schema auto generation?
In this special case: the field is inherited from a parent abstract class, so I cannot just comment it out!
I tried using #Transient, but the field is still autogenerated in the schema.
#MappedSuperclass
public abstract class BaseEntity {
private String someField;
//getter+setter
}
#Entity
public class MyEntity extends BaseEntity {
#Transient //I want to ignore this field during hibernate.ddl.auto
#Override
public String getSomeField() {}
}
Add the Transient annotation in the super class:
#MappedSuperclass
public abstract class BaseEntity {
#Transient
private String someField;
}
#Entity
public class MyEntity extends BaseEntity {
}
Related
I have the following superclass that will be extended by many classes. Its one property, foreignKey, will be the #Id for one of the inheriting classes.
The superclass:
#MappedSuperclass
public abstract Superclass {
#Column(name="FOREIGN_KEY")
private String foreignKey;
// Getters and setters
}
The inheriting class:
#Entity
public class ClassA extends Superclass {
#Id
#Column(name="FOREIGN_KEY")
private String foreignKey;
// Getters and setters
}
When I try building I am given the error:
No identifier specified for ClassA
Is there anyway to accomplish this?
I am new with JPA, so maybe someone can explain me how to correctly annotate abstract classes using JPA?
I have an abstract class with generated id field:
public abstract class AbstractClass implements Serializable {
private static final long serialVersionUID = 1L;
private long id;
#Id
#GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
and an abstract class with name which extends AbstractClass:
public abstract class AbstractNameClass extends AbstractClass {
private String shortName;
#Column(name = "shortName", nullable = false)
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
}
I have two types of classes, one extends AbstractClass and other classes extends AbstractNameClass:
#Entity
public class Model extends AbstractNameClass {
// this class should inherit id (from AbstractClass) and shortName (from AbstractNameClass)
}
and
#Entity
public class Vehicle extends AbstractClass {
// this class should inherit only id
}
If I add #MappedSuperclass annotation on AbstractClass, then I can create and save objects which are extending AbstractClass, but how to annotate AbstractNameClass? I tried to add #Entity annotation, but I got "No identifier specified for entity" error, also I tried to add #MappedSuperclass annotation and also got the same error.
So my question would be - how to correctly annotate abstract classes using JPA, without creating AbstractClass and AbstractNameClass tables (in my db I want to have only Model and Vehicle tables)?
I want to use Hibernate. I have a database schema and I would like to write annotations in my code.
I have az A class. It's look like this:
In A.java:
public class A {
public Integer id;
}
In B.java:
#Entity
#Table(name="table_b")
public class B extends A {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
//How can I get that id attribute from the A class?
#Column
public String string;
}
you put the annotation #MappedSuperClass above class A. The field id has to be annotated in clas A not B.
I have a problem, I have two entity Job and JobPK
Job class looks like this sample code :
#Entity
#IdClass(JobPK.class)
#Table(name="JOB")
#Inheritance
#DiscriminatorColumn(name="JOB_TYPE")
public abstract class Job implements Serializable {
#Id
#Column(name="FOLDER_ID")
private BigDecimal folderId;
#Id
#ColumnDefinition(position = 1)
private String name;
#Column(name="JOB_TYPE",insertable=false,updatable=false)
private String jobType;
...
}
and JobPk :
public class JobPK implements Serializable {
private static final long serialVersionUID = -3266336718203527905L;
#Column(name="JOB_TYPE",insertable=false,updatable=false)
private String jobType;
#Id
private String name;
#Id
#Column(name="FOLDER_ID")
private BigDecimal folderId;
......
}
I have two class which extends Job : CalculatingJob and ImportingJob
Now I wont to use :
getEntityManager().find(CalculatingJob.class, new JobPK (BigDecimal.valueOf(folderId),name))
and I have problem because I must fill i JobPK descriminator value field. If I don't do that I've got Null Pointer Exception. Descriminator value is in key by default I think but I don't want put information about descriminator value explicite during JobPk creating. I thought that Entity which extends from Job will fill this field automaticaly. Any Idea to bypass this problem, maybe I can get Annotation #DescriminatorVale from CalculatingJob and then put into constructor JobPk
Thanks for Help
Try this configuration for Hierarchy structure
Job.java
#Table(name = "JOB")
#Inheritance
#IdClass(JobPK.class)
#DiscriminatorColumn(name = "JOB_TYPE", discriminatorType = DiscriminatorType.STRING)
public abstract class Job implements java.io.Serializable {
}
CalculatingJob.java
#Entity
#DiscriminatorValue("CalculatingJob")
public class CalculatingJob extends Job {
}
ImportingJob.java
#Entity
#DiscriminatorValue("ImportingJob")
public class ImportingJob extends Job {
}
JobPK.java
public class JobPK implements Serializable {
}
The discriminator value is entered by hibernate.
I have the below scenario, I need sub-classes to be able to specify the actual types of properties found in MappedSuperClass(s). I use hibernate as a provider and I don't mind using hibernate specific annotations to solve this problem.
#MappedSuperclass
abstract class BaseA{
....
#OneToMany(mappedBy = "baseA")
public Set<? extends BaseB> getBaseB(){
.....
}
}
#MappedSuperclass
abstract class BaseB{
.....
#ManyToOne(optional = false)
#JoinColumn(name = "basea_id")
public BaseA getBaseA(){
.....
}
}
#Entity
class BaseAImpl extends BaseA{
public Set<BaseBImpl> getBaseB(){
.....
}
}
#Entity
class BaseBImpl{
public BaseAImpl getBaseA(){
.....
}
}
#AssociationOverride will hep you in this case. See the documentation for it (of course it is a JPA annotation). You could use it in combination with the #AttributeOverrides annotation for overriding basic types. Example (taken from the example):
#MappedSuperclass
public class Employee {
...
#ManyToOne
protected Address address;
...
}
#Entity
#AssociationOverride(name="address",
joinColumns=#JoinColumn(name="ADDR_ID"))
// address field mapping overridden to ADDR_ID foreign key
public class PartTimeEmployee extends Employee {
...
}