annotation hibernate pojo

问题描述

我有2张表source表user表sidintprimarykey,uidintprimarykey,namevarchar(12),namevarchar(12),u_idintforeignkeyreferencesusersource多对一user帮我用annotation写上面的pojo类要双向关联

解决方案

解决方案二:
User表:importjava.util.HashSet;importjava.util.Set;importjavax.persistence.CascadeType;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.FetchType;importjavax.persistence.Id;importjavax.persistence.OneToMany;importjavax.persistence.Table;/***Userentity.@authorMyEclipsePersistenceTools*/@Entity@Table(name="user",catalog="struts2jpa")publicclassUserimplementsjava.io.Serializable{//FieldsprivateIntegeruid;privateStringname;privateSet<Source>sources=newHashSet<Source>(0);//Constructors/**defaultconstructor*/publicUser(){}/**minimalconstructor*/publicUser(Integeruid){this.uid=uid;}/**fullconstructor*/publicUser(Integeruid,Stringname,Set<Source>sources){this.uid=uid;this.name=name;this.sources=sources;}//Propertyaccessors@Id@Column(name="uid",unique=true,nullable=false)publicIntegergetUid(){returnthis.uid;}publicvoidsetUid(Integeruid){this.uid=uid;}@Column(name="name",length=12)publicStringgetName(){returnthis.name;}publicvoidsetName(Stringname){this.name=name;}@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="user")publicSet<Source>getSources(){returnthis.sources;}publicvoidsetSources(Set<Source>sources){this.sources=sources;}}

source表:importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.FetchType;importjavax.persistence.Id;importjavax.persistence.JoinColumn;importjavax.persistence.ManyToOne;importjavax.persistence.Table;@Entity@Table(name="source",catalog="struts2jpa")publicclassSourceimplementsjava.io.Serializable{//FieldsprivateIntegersid;privateUseruser;privateStringname;//Constructors/**defaultconstructor*/publicSource(){}/**minimalconstructor*/publicSource(Integersid){this.sid=sid;}/**fullconstructor*/publicSource(Integersid,Useruser,Stringname){this.sid=sid;this.user=user;this.name=name;}//Propertyaccessors@Id@Column(name="sid",unique=true,nullable=false)publicIntegergetSid(){returnthis.sid;}publicvoidsetSid(Integersid){this.sid=sid;}@ManyToOne(fetch=FetchType.LAZY)@JoinColumn(name="u_id")publicUsergetUser(){returnthis.user;}publicvoidsetUser(Useruser){this.user=user;}@Column(name="name",length=12)publicStringgetName(){returnthis.name;}publicvoidsetName(Stringname){this.name=name;}}

解决方案三:
差不多就是LS的了,有一点可以说明一下@ManyToOne(fetch=FetchType.LAZY)也可以不采用延迟加载
解决方案四:
有没有一种工具可以直接通过表生成上面的jpa啊

时间: 2024-08-08 16:39:57

annotation hibernate pojo的相关文章

使用Spring的JMX annotation让POJO对象输出到JMX

自JDK5.0 引入注解(Annotation)后,让Java的开发简化了很多,让开发者几 乎摆脱复杂的 配置文件的烦恼.本文将介绍Spring提供的一套相当于Commons Attribut属 性类的注解和一个策略接口 JmxAttributeSource 的实现类 AnnotationsJmxAttributeSource, 这个类允许 MBeanInfoAssembler 来读这些 注解.本文就给大家展示一下,使用Spring的JMX annotation,如何简单快速让 POJO对象输出

补课了,30分钟让你学会java自定义annotation

自定义Annotation概念篇 来看一个最简单的annotation package com.cts.elt.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; importjava.lang.annotation.RetentionPolicy; import java.lang.a

java Hibernate框架Session的保存、更新、删除和查询教程

session的save方法 使一个临时对象变为持久化对象.session的save方法完成以下操作:1.把customer对象加入到缓存中,使他变为持久化对象2.选用映射文件指定的标识符生成器为持久化对象分配唯一的OID.Customer.hbm.xml文件中id元素的子元素指定标识符生成器: <generator><id name="id" column="ID" >    <generator class="increm

hibernate 保存对象报的一个异常 让人迷糊

问题描述 异常信息:log4j:WARNNoappenderscouldbefoundforlogger(org.hibernate.cfg.Environment).log4j:WARNPleaseinitializethelog4jsystemproperly.Hibernate:selectDEPARTMENT_ID_SEQ.nextvalfromdualHibernate:insertintoStudent(name,age,_id)values(?,?,?)Exceptioninthr

Hibernate使用二级缓存的问题.请大家帮忙看下。

问题描述 我在做Hibernate中做测试的时候 ,使用的是Hibernate3.32版本,在Hibernate.cfg.xml文件中配置了二级缓存.但是在不同的条件下查询的时候,会从数据库里面查询,不会从二级缓存中取出数据, 我看了一下我的打印出的log信息.有两条sql语句.下面是我的Hibernate.cfg.xml配置文件:<session-factory><property name="connection.username">root</pro

用AJAX实现例如百度搜索的提示功能遇到的问题

问题描述 首先写了一个test.jsp代码如下:<%@pagelanguage="java"pageEncoding="GBK"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head>

hibernate4 存储图片

问题描述 org.hibernate.exception.SQLGrammarException:YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'?1姯?-駭0⒗S=i偋??G伤G?翤篘+O綨'O糿視剈股摦+縶怽'???[08榅?f'atline1用blob类型存储图片时,出现以上异常,字段检查无误,没有包含关键字存储文本可以

hibernate中用annotation注解怎么样配置TIMESTAMP类型的数据呢

问题描述 请问hibernate中用annotation注解怎么样配置TIMESTAMP类型的数据并且能自动插入和更新呢?就是相当于mysql中timeTIMESTAMPDEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMP;这样的数据列,在hibernate中怎么样用annotation注解来配置呢(不要xml的配置方式).各位大虾帮帮忙啊,跪求中-- 解决方案 解决方案二:没用过注解吖.看下这个资料吧解决方案三:@Column(name="D_UP

hibernate(jpa)复合主键annotation声明方法

在设计数据库表的时候,往往会设计出带有复合主键的表,即表的记录由多 个字段联合标识,如: 表 CREATE TABLE TB_HOUR_DATA ( STAT_DATE DATE NOT NULL, PATH_ID NUMBER(20) NOT NULL, VALUE VARCHAR2(512 BYTE), TYPE NUMBER(1) NOT NULL ) 其中,复合主键为(STAT_DATE,PATH_ID,TYPE) 针对这种情况,hibernate(jpa) 的 annotation 映