I have 2 tables, devices which contains a list of devices and dev_tags, which contains a list of asset tags for these devices. The tables join on dev_serial_num, which is the primary key of neither table. The devices are unique on their ip_address field and they have a primary key identified by dev_id. The devices "age out" after 2 weeks. Therefore, the same piece of hardware can show up more than once in devices.
I mention that to explain why there is a OneToMany relationship between dev_tags and devices where it seems that this should be a OneToOne relationship.
So I have my 2 entities
#Entity
#Table(name = "dev_tags")
public class DevTags implements Serializable {
private Integer tagId;
private String devTagId;
private String devSerialNum;
private List<Devices> devices;
#Id
#GeneratedValue
#Column(name = "tag_id")
public Integer getTagId() {
return tagId;
}
public void setTagId(Integer tagId) {
this.tagId = tagId;
}
#Column(name="dev_tag_id")
public String getDevTagId() {
return devTagId;
}
public void setDevTagId(String devTagId) {
this.devTagId = devTagId;
}
#Column(name="dev_serial_num")
public String getDevSerialNum() {
return devSerialNum;
}
public void setDevSerialNum(String devSerialNum) {
this.devSerialNum = devSerialNum;
}
#OneToMany(mappedBy="devSerialNum")
public List<Devices> getDevices() {
return devices;
}
public void setDevices(List<Devices> devices) {
this.devices = devices;
}
}
and this one
public class Devices implements java.io.Serializable {
private Integer devId;
private Integer officeId;
private String devSerialNum;
private String devPlatform;
private String devName;
private OfficeView officeView;
private DevTags devTag;
public Devices() {
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "dev_id", unique = true, nullable = false)
public Integer getDevId() {
return this.devId;
}
public void setDevId(Integer devId) {
this.devId = devId;
}
#Column(name = "office_id", nullable = false, insertable=false, updatable=false)
public Integer getOfficeId() {
return this.officeId;
}
public void setOfficeId(Integer officeId) {
this.officeId = officeId;
}
#Column(name = "dev_serial_num", nullable = false, length = 64, insertable=false, updatable=false)
#NotNull
#Length(max = 64)
public String getDevSerialNum() {
return this.devSerialNum;
}
public void setDevSerialNum(String devSerialNum) {
this.devSerialNum = devSerialNum;
}
#Column(name = "dev_platform", nullable = false, length = 64)
#NotNull
#Length(max = 64)
public String getDevPlatform() {
return this.devPlatform;
}
public void setDevPlatform(String devPlatform) {
this.devPlatform = devPlatform;
}
#Column(name = "dev_name")
public String getDevName() {
return devName;
}
public void setDevName(String devName) {
this.devName = devName;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "office_id")
public OfficeView getOfficeView() {
return officeView;
}
public void setOfficeView(OfficeView officeView) {
this.officeView = officeView;
}
#ManyToOne()
#JoinColumn(name="dev_serial_num")
public DevTags getDevTag() {
return devTag;
}
public void setDevTag(DevTags devTag) {
this.devTag = devTag;
}
}
I messed around a lot with #JoinColumn(name=) and the mappedBy attribute of #OneToMany and I just cannot get this right. I finally got the darn thing to compile, but the query is still trying to join devices.dev_serial_num to dev_tags.tag_id, the #Id for this entity. Here is the transcript from the console:
13:12:16,970 INFO [STDOUT] Hibernate:
select
devices0_.office_id as office5_2_,
devices0_.dev_id as dev1_2_,
devices0_.dev_id as dev1_156_1_,
devices0_.dev_name as dev2_156_1_,
devices0_.dev_platform as dev3_156_1_,
devices0_.dev_serial_num as dev4_156_1_,
devices0_.office_id as office5_156_1_,
devtags1_.tag_id as tag1_157_0_,
devtags1_.comment as comment157_0_,
devtags1_.dev_serial_num as dev3_157_0_,
devtags1_.dev_tag_id as dev4_157_0_
from
ond.devices devices0_
left outer join
ond.dev_tags devtags1_
on devices0_.dev_serial_num=devtags1_.tag_id
where
devices0_.office_id=?
13:12:16,970 INFO [IntegerType] could not read column value from result set: dev4_156_1_; Invalid value for getInt() - 'FDO1129Y2U4'
13:12:16,970 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: S1009
13:12:16,970 ERROR [JDBCExceptionReporter] Invalid value for getInt() - 'FDO1129Y2U4'
That value for getInt() 'FD01129Y2U4' is a serial number, definitely not an Int! What am I missing/misunderstanding here? Can I join 2 tables on any fields I want or does at least one have to be a primary key?
The short answer is "no, you can't join 2 tables on any fields"; association will always refer to primary key on one side.
"mappedBy" attribute for #OneToMany is used for bi-directional assocations and specifies the name of the property on collection element that maps back to owner entity as #ManyToOne. In your case,
#OneToMany(mappedBy="devSerialNum")
declaration is invalid; it should be changed to
#OneToMany(mappedBy="devTag")
instead if you want to maintain a bi-directional relationship. #JoinColumn can be used with #ManyToOne to specify the name of the (foreign key) column pointing to the other table. In your case,
#ManyToOne()
#JoinColumn(name="dev_serial_num")
public DevTags getDevTag() {
declaration says that you have a column called dev_serial_num in your devices table that will be a foreign key pointing to dev_tags.tag_id which is also wrong.
I'm not quite clear on what you meant by "devices age out", but it seems to me that you're trying to merge two separate concepts into a single table which is where all these issues stem from. Consider instead separating your "devices" table (and entity) into two:
"Core" device (for the lack of better name) should contain truly unique attributes, like serial number. Your DevTags entity will be linked as one-to-many to this one.
Device "version" would contain attributes applicable to individual "version". There will be multiple "versions" for each "core" device; "version" is what will be updated every 2 weeks.
Related
I have a enum of few status value
NEW, REVIEWD, PUBLISHED, PENDING, UPDATED, SPAM, DUPLICATE, IRRELEVANT, UNPUBLISHED
I don't want to use them as enumerated so created one entity for that. For convenient I want to keep a column in entity to initialize status from enum and convert that enumerated value to a Object of status entity. for this..
I have two entity. I want to refer a column with value from another entity.
Basically I want to initialize a object with formula.
Entities are
#Entity
#Table(name = "event_status")
public class EventStatus {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name="eventStatusId")
private Integer eventStatusId;
#Enumerated(EnumType.STRING)
#Column(unique = true,name="eventStatusType")
private EventStatusType eventStatusType;
public EventStatus() {
this(EventStatusType.NEW);
}
public EventStatus(EventStatusType eventStatusType) {
super();
this.eventStatusType = eventStatusType;
}
public Integer getEventStatusId() {
return eventStatusId;
}
public EventStatusType getEventStatusType() {
return eventStatusType;
}
public void setEventStatusId(Integer eventStatusId) {
this.eventStatusId = eventStatusId;
}
public void setEventStatusType(EventStatusType eventStatusType) {
this.eventStatusType = eventStatusType;
}
}
I have another entity in which I am referring object of this entity
#Entity
#Table(name = "event_")
#Inheritance(strategy = InheritanceType.JOINED)
public abstract class Event implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name = "id_")
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Transient
public EventStatusType eventStatusType = EventStatusType.NEW;
#ManyToOne(fetch = FetchType.EAGER, targetEntity = EventStatus.class)
#Formula("select * from event_status where eventStatusId= 1")
private EventStatus status;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public EventStatus getStatus() {
System.out.println("Event.getStatus() " + status);
return status;
}
public void setStatus(EventStatus status) {
System.out.println("Event.setStatus()");
this.status = status;
}
}
This is not giving any exception but not initializing this value.
Is it possible to initialize this EntityStatus with value of eventStatusType in Event entity
I would like to explain that based on the documentation:
5.1.4.1.5. Formula
Sometimes, you want the Database to do some computation for you rather than in the JVM, you might also create some kind of virtual column. You can use a SQL fragment (aka formula) instead of mapping a property into a column. This kind of property is read only (its value is calculated by your formula fragment).
#Formula("obj_length * obj_height * obj_width")
public long getObjectVolume()
The SQL fragment can be as complex as you want and even include subselects.
...
5.1.7.1. Using a foreign key or an association table
...
Note
You can use a SQL fragment to simulate a physical join column using the #JoinColumnOrFormula / #JoinColumnOrformulas annotations (just like you can use a SQL fragment to simulate a property column via the #Formula annotation).
#Entity
public class Ticket implements Serializable {
#ManyToOne
#JoinColumnOrFormula(formula="(firstname + ' ' + lastname)")
public Person getOwner() {
return person;
}
...
}
Also, we should use insertable = false, updatable = false, because such mapping is not editable
My two tables (in SQL Server):
create table cluster (
id bigint primary key identity(1,1),
name varchar(100)
)
create table cluster_member (
cluster_id bigint,
member_name varchar(100)
)
The table cluster_member doesn't have an id. The column cluster_id is like a foreign key, reference to the id column in cluster table.
I used Hiberate Tools to generate 2 #Entity classes and a #Embeddable class. I added some class variables and #OneToMany and #ManyToOne annotations trying to join the two tables. But I got an error saying:
org.hibernate.MappingException: Foreign key (FK_hk6sas3oycvcljwbjar7p9ky3:cluster_member [cluster_id,member_name])) must have same number of columns as the referenced primary key (cluster [id])
The error message is pretty clear. But I don't know how to fix it. Please help.
Here is my code:
Cluster.java:
#Entity
#Table(name = "cluster" )
public class Cluster implements java.io.Serializable {
private long id;
private String name;
private Set<ClusterMember> members;
#Id
#Column(name = "id", unique = true, nullable = false)
public long getId() {
return this.id;
}
#Column(name = "name", length = 100)
public String getName() {
return this.name;
}
#OneToMany(mappedBy = "id")
public Set<ClusterMember> getMembers() {
return members;
}
}
ClusterMember.java:
#Entity
#Table(name = "cluster_member" )
public class ClusterMember implements java.io.Serializable {
private ClusterMemberId id;
private Cluster cluster;
#EmbeddedId
#AttributeOverrides({ #AttributeOverride(name = "clusterId", column = #Column(name = "cluster_id")),
#AttributeOverride(name = "memberName", column = #Column(name = "member_name", length = 100)) })
public ClusterMemberId getId() {
return this.id;
}
#ManyToOne
#JoinColumn(name = "cluster_id")
public Cluster getCluster() {
return cluster;
}
}
ClusterMemberId.java:
#Embeddable
public class ClusterMemberId implements java.io.Serializable {
private Long clusterId;
private String memberName;
#Column(name = "cluster_id")
public Long getClusterId() {
return this.clusterId;
}
#Column(name = "member_name", length = 100)
public String getMemberName() {
return this.memberName;
}
}
main function:
#SuppressWarnings("deprecation")
public static void main(String[] args) {
sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria("my.hibernate.table.Cluster");
criteria.add(Restrictions.like("name", "%ABC%"));
#SuppressWarnings("unchecked")
List<Cluster> clusters = criteria.list();
for (Cluster cluster: clusters) {
System.out.println(cluster.toString());
}
tx.commit();
sessionFactory.close();
}
hibernate.cfg.xml
<mapping class="my.hibernate.table.Cluster" />
<mapping class="my.hibernate.table.ClusterMember" />
Try changing this:
#OneToMany(mappedBy = "id")
public Set<ClusterMember> getMembers() {
return members;
}
to
#OneToMany(mappedBy = "cluster")
public Set<ClusterMember> getMembers() {
return members;
}
and add insertable/updatable to false on the associated ManyToOne mapping.
#ManyToOne
#JoinColumn(name = "cluster_id", insertable="false", updatable="false")
public Cluster getCluster() {
return cluster;
}
Because you are not really interested in the ClusterMember.id but in the FK linking back to Cluster.
In Hibernate you cannot use the same column in to different mapping. The "ClusterMember" already uses "cluster_id" for the #Id property, hence if you plan on using for a ManyToOne association, you need to instruct Hibernate to ignore any changes to this end (inserts and updates should be ignored).
Also you can use Hibernate's #MapsId annotation, for composite identifiers with alternate associated mappings.
I have the following files in Play Framework 2.2.3
Controller:
public class Comment extends Controller
{
public Result create(UUID id)
{
models.blog.Blog blog = models.blog.Blog.finder.byId(id);
Result result;
if(blog == null)
{
result = notFound(main.render("404", error404.render()));
}
else
{
Form<models.blog.Comment> commentForm = Form.form(models.blog.Comment.class);
commentForm = commentForm.bindFromRequest();
if(commentForm.hasErrors())
{
result = badRequest(Json.toJson(commentForm));
}
else
{
models.blog.Comment comment = commentForm.get();
comment.setId(UUID.randomUUID());
comment.setTimeCreated(new Date());
comment.setBlogId(blog.getId());
comment.save();
result = ok(Json.toJson(comment));
}
}
return result;
}
}
And two models
#Entity
#Table(name="blog")
public class Blog extends Model
{
private static final SimpleDateFormat MONTH_LITERAL = new SimpleDateFormat("MMMMM"),
DAY_NUMBER = new SimpleDateFormat("d"),
YEAR_NUMBER = new SimpleDateFormat("yyyy");
public static Finder<UUID, Blog> finder = new Finder<UUID, Blog>(UUID.class, Blog.class);
#Id
#Column(name="id",length=36, nullable=false)
public UUID id;
#OneToOne
#JoinColumn(name="author_id")
public User author;
#Column(name="title",length=255)
public String title;
#Column(name="summary",length=255)
public String summary;
#Column(name="url",length=255)
public String url;
#Column(name="content")
public String content;
#Column(name="time_updated")
public Date time_created;
#Column(name="time_created", nullable=false)
public Date time_updated;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name="blog_id")
public List<Comment> comments;
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(
name="blog_tag_map",
joinColumns={ #JoinColumn(name="blog_id", referencedColumnName="id") },
inverseJoinColumns={ #JoinColumn(name="tag_id", referencedColumnName="id") }
)
public List<Tag> tags;
public List<Comment> getComments()
{
return this.comments;
}
}
#Entity
#Table(name="blog_comment")
public class Comment extends Model
{
private static final SimpleDateFormat MONTH_LITERAL = new SimpleDateFormat("MMMMM"),
DAY_NUMBER = new SimpleDateFormat("d"),
YEAR_NUMBER = new SimpleDateFormat("yyyy");
#Id
#Column(name="id",length=36, nullable=false)
public UUID id;
#Column(name="blog_id", length=36)
public UUID blog_id;
#ManyToOne
public Blog blog;
#Column(name="content", length=500)
public String content;
#Column(name="website", length=255)
public String website;
#Column(name="name", length=255)
public String name;
#Column(name="time_created", updatable=false)
public Date time_created;
}
I have excluded some setters and getters from these models for brevity, so it doesn't clog up this post.
When I attempt to make a POST request to the aforementioned controller, everything goes fine until I get to the "comment.save()" statement in the controller file, then I get the following error.
I'm unsure why this save isn't going through, and why there is a column conflict.
Help much appreciated
The issue lies in the fact that you have defined basically two foreign key columns for Blog in your Comment's entity:
#Column(name = "blog_id", length = 36)
public UUID blog_id;
#ManyToOne
public Blog blog;
The default column name for your 'blog' field is: blog_id
However, you've already named your 'blog_id' column that.
Interestingly, no error/warning is thrown when creating this table...
So when you call comment.save(), the following insert statement is generated:
insert into blog_comment (id, blog_id, content, website, name, time_created, blog_id) values (?,?,?,?,?,?,?)
Notice a reference to 'blog_id' column twice, which is invalid.
And this is because of the above double mapping.
To fix, just give your 'blog' property a different name to use for the foreign key column:
#Column(name = "blog_id", length = 36)
public UUID blog_id;
#ManyToOne
#JoinColumn(name = "blogId")
public Blog blog;
I'm not sure why you're mapping your entities like this (perhaps legacy schema?) but the 'blog_id' fields seem to be redundant (and confusing) as you already have an entity mapping in the form of your 'blog' property.
This question is pretty old, but for any future reference i have found this answer that solved my problem.
After numerous searchers around the web I found this answer here - thanks to jtal!
Just to summaries the problem:
Using Ebean i have made a #ManyToOne entity that is not implemented in the database in anyway,
even more the join field, in your case
blogId
is a valid field that has values of its own.
when trying to join the column on that field, it will always fail because it creates this sql query:
SELECT
*
FROM
blog_comment;
select
t0.id c0,
t0.blog_id c1,
t0.content c2,
t0.website c3,
t0.time_created c4,
t0.blog_id c5 <---- notice this duplicate
from
blog_comment t0
in order to solve this, i tell ebean not to use the second set of properties.
your new ebean element should look something like this:
#ManyToOne
#JoinColumn(name = "blogId", insertable = false, updatable = false)
public Blog blog;
hope this helps! =)
I want to join two tables on column "vendor",
In invoice table vendor type is integer, in vendor table, vendor is type varchar(10).
Is it possible to do a type conversion and also have a relationship?
#Entity
public class Vendor
{
private String id;
#Id(Column="vendor")
public String getId(){ ... }
}
#Entity
public class Invoice
{
private Vendor vendor;
#One-to-one
public Vendor getVendor() { ... }
}
As far as I know, using a pivot table (as you would do to represent a many-to-many relationship) would be the right way to do that.
Something like this should work:
#Entity
public class Invoice
{
#JoinTable(name = "invoice_vendor", joinColumns = {
#JoinColumn(name = "invoice", referencedColumnName = "vendor_id")}, inverseJoinColumns = {
#JoinColumn(name = "vendor", referencedColumnName = "id")})
#OneToOne
private Vendor vendor;
}
Where the invoice_vendor table has the integer id in id column and varchar reference in vendor_id column.
Also I surmise you would like a ManyToOne relationship between vendors, but you've written one to one, so I have left that as such.
Maybe this can be done using a transient field
#Entity
public class Employee {
...
private boolean isActive;
...
#Transient
public boolean getIsActive() {
return isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
#Basic
private String getIsActiveValue() {
if (isActive) {
return "T";
} else {
return "F";
}
}
private void setIsActiveValue(String isActive) {
this.isActive = "T".equals(isActive);
}
}
http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes#Conversion
Which JPA provider do you use?
It seems that Hibernate has a separate annotation for this (#JoinColumnsOrFormula). To my knowledge, EclipseLink does not offer that annotation.
See related question on stackoverflow
Consider the following table:
CREATE TABLE Participations
(
roster_id INTEGER NOT NULL,
round_id INTEGER NOT NULL,
ordinal_nbr SMALLINT NOT NULL ,
was_withdrawn BOOLEAN NOT NULL,
PRIMARY KEY (roster_id, round_id, ordinal_nbr),
CONSTRAINT participations_rosters_fk FOREIGN KEY (roster_id) REFERENCES Rosters (id),
CONSTRAINT participations_groups_fk FOREIGN KEY (round_id, ordinal_nbr) REFERENCES Groups (round_id , ordinal_nbr)
)
Here the JPA 1.0 #IdClass entity class:
#Entity
#Table(name = "Participations")
#IdClass(value = ParticipationId.class)
public class Participation implements Serializable
{
#Id
#Column(name = "roster_id", insertable = false, updatable = false)
private Integer rosterId;
#Id
#Column(name = "round_id", insertable = false, updatable = false)
private Integer roundId;
#Id
#Column(name = "ordinal_nbr", insertable = false, updatable = false)
private Integer ordinalNbr;
#Column(name = "was_withdrawn")
private Boolean wasWithdrawn;
#ManyToOne
#JoinColumn(name = "roster_id", referencedColumnName = "id")
private Roster roster = null;
#ManyToOne
#JoinColumns(value = {#JoinColumn(name = "round_id", referencedColumnName = "round_id"), #JoinColumn(name = "ordinal_nbr", referencedColumnName = "ordinal_nbr")})
private Group group = null;
public Participation()
{
}
public Integer getRosterId()
{
return rosterId;
}
public void setRosterId(Integer rosterId)
{
this.rosterId = rosterId;
}
public Integer getRoundId()
{
return roundId;
}
public void setRoundId(Integer roundId)
{
this.roundId = roundId;
}
public Integer getOrdinalNbr()
{
return ordinalNbr;
}
public void setOrdinalNbr(Integer ordinalNbr)
{
this.ordinalNbr = ordinalNbr;
}
public Boolean getWasWithdrawn()
{
return wasWithdrawn;
}
public void setWasWithdrawn(Boolean wasWithdrawn)
{
this.wasWithdrawn = wasWithdrawn;
}
public Roster getRoster()
{
return roster;
}
// ???
public void setRoster(Roster roster)
{
this.roster = roster;
}
public Group getGroup()
{
return group;
}
// ???
public void setGroup(Group group)
{
this.group = group;
}
...
}
In general, should the association setters synchronize with the redundant fields, here rosterId, roundId, and ordinalNbr?:
// ???
public void setGroup(Group group)
{
this.group = group;
this.roundId = group.getRoundId();
this.ordinalNbr = group.getOrdinalNbr();
}
Thanks
Yes, they should be kept in synch. Although because they are part of the Id you should never be changing these, so it is really only an issue for new objects.
If you do not keep them in synch, then for a new object they will be null/0, which is probably not good. There is no magic in JPA that will keep these in synch for you.
If you read the object from the database, then they will be in synch of coarse, but you are responsible for maintaining your object's state once in memory, including both duplicate fields, and bi-directional mappings.
If you are using JPA 2.0, why bother having the duplicate Ids at all. You can remove the routersId and the roundId and just add the #Id to the #ManyToOnes.