我有2个班:司机和汽车.汽车表在单独的过程中更新.我需要的是在驱动程序中拥有属性,允许我读取完整的汽车描述并只写入指向现有汽车的Id.这是一个例子:
@Entity(name = "DRIVER")
public class Driver {
... ID and other properties for Driver goes here .....
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "CAR_ID")
private Car car;
@JsonView({Views.Full.class})
public Car getCar() {
return car;
}
@JsonView({Views.Short.class})
public long getCarId() {
return car.getId();
}
public void setCarId(long carId) {
this.car = new Car (carId);
}
}
Run Code Online (Sandbox Code Playgroud)
Car对象只是典型的JPA对象,没有对Driver的反向引用.
所以我试图通过这个实现:1)我可以使用详细的JSON View 2)阅读完整的汽车描述或者我只能阅读简短的JsonView 3中的汽车ID而且最重要的是,当创建新的驱动程序我只是想要传递汽车的JSON ID.这样我就不需要在持久化期间为汽车执行不必要的读取操作,而只是更新Id.
我得到以下错误:"对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.Driver.car - > com.Car"
我不想在DB中更新Car的实例,而只是从Driver中引用它.知道怎么做到我想要的吗?
谢谢.
更新:忘记提及我在创建驱动程序期间传递的汽车ID是DB中现有汽车的有效ID.
我有一个Doctrine模型(Assignment),它与另一个模型(Region)有多对一的关系.分配由用户拥有(每个用户一次只能为每个区域分配一个分配),我试图用来indexBy分配用户的分配数组,并使用分配区域的ID.但是,我只获得标准的0..n数字键.
当我尝试运行DQL查询时SELECT am, reg, user FROM Assignment am INDEX BY [...] JOIN am.region reg JOIN am.user user WHERE user.id = ?1,INDEX BY的这些值都不起作用:
region (错误:PathExpression无效.必须是StateFieldPathExpression.)region_id (错误:类... \赋值没有名为region_id的字段或关联)region.id (错误:字符串的预期结束,得到'.')这可能吗?如果没有,那么User在没有区域的情况下访问某个区域的分配的便捷方式是indexBy什么?
我有一个将一个数据库转换为另一个数据库的项目 其中一个原始数据库列定义行的类别.此列应映射到新数据库中的新类别.
例如,我们假设原始类别是:parrot, spam, cheese_shop, Cleese, Gilliam, Palin
现在这对我来说有点冗长,而且我希望将这些行分类为sketch, actor- 也就是说,将所有草图和所有actor定义为两个等价类.
>>> monty={'parrot':'sketch', 'spam':'sketch', 'cheese_shop':'sketch',
'Cleese':'actor', 'Gilliam':'actor', 'Palin':'actor'}
>>> monty
{'Gilliam': 'actor', 'Cleese': 'actor', 'parrot': 'sketch', 'spam': 'sketch',
'Palin': 'actor', 'cheese_shop': 'sketch'}
Run Code Online (Sandbox Code Playgroud)
这很尴尬 - 我更喜欢这样的东西:
monty={ ('parrot','spam','cheese_shop'): 'sketch',
('Cleese', 'Gilliam', 'Palin') : 'actors'}
Run Code Online (Sandbox Code Playgroud)
但是,这当然将整个元组设置为关键:
>>> monty['parrot']
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
monty['parrot']
KeyError: 'parrot'
Run Code Online (Sandbox Code Playgroud)
如何在Python中创建优雅的多对一字典?
谢谢,
亚当
我试图通过Symfony2(2.3.0)使用Doctrine(2.2.3+)在我的数据库中的对象上建立一些ManyToOne/OneToMany关系,并得到一个奇怪的错误.以下是对象的相关部分(一个产品的许多属性):
/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity
*/
class Product
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
...
/**
*
* @OneToMany(targetEntity="ProductAttributes", mappedBy="product")
*/
protected $product_attributes;
public function __construct() {
$this->product_attributes = new \Doctrine\Common\Collections\ArrayCollection();
}
}
/**
* ProductAttributes
*
* @ORM\Table(name="product_attributes")
* @ORM\Entity
*/
class ProductAttributes
{
/**
* @var integer
*
* @ORM\Column(name="pa_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $pa_id;
/**
* @var integer
*
* …Run Code Online (Sandbox Code Playgroud) 我正在使用Hibernate和spring.
我有这样的模型类.
@Entity
@Table(name = "forumtopic")
public final class Forumtopic extends AbstractUserTracking implements
java.io.Serializable {
/**SNIP **/
private Forumcategory forumcategory;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FkForumcategoryId", nullable = false)
public Forumcategory getForumcategory() {
return this.forumcategory;
}
public void setForumcategory(final Forumcategory forumcategory) {
this.forumcategory = forumcategory;
}
}
Run Code Online (Sandbox Code Playgroud)
它一般工作,但类不是懒惰加载,但在加载ForumEntry后急切.
Hibernate:
select
forumtopic0_.PkId as PkId19_0_,
forumtopic0_.CreateDate as CreateDate19_0_,
forumtopic0_.FkCreateUserId as FkCreate3_19_0_,
forumtopic0_.FkLastUserId as FkLastUs4_19_0_,
forumtopic0_.LastChange as LastChange19_0_,
forumtopic0_.FkForumcategoryId as FkForum10_19_0_,
forumtopic0_.PublishCategory as PublishC6_19_0_,
forumtopic0_.State as State19_0_,
forumtopic0_.Text as Text19_0_,
forumtopic0_.Topic as Topic19_0_, …
请指教我如何从many2one字段中删除"创建和编辑...".该项目显示在我使用域选项过滤的many2one字段中.
OpenERP第7版
我有一个复合键ContractServiceLocationPK做出来的三个ID.( ,,contractId 在嵌入类型长).使用此复合键的类使用注释将这些ID映射到其对象.这是它的样子(删除了setter/getters和不相关的属性):locationIdserviceIdContractServiceLocation@MapsId
合同
@Entity
@Table(name = "Contract")
public class Contract implements Serializable {
public Contract() {
}
@Id
@GeneratedValue
private long id;
@OneToMany(mappedBy = "contract", cascade = CascadeType.ALL, fetch= FetchType.EAGER)
Collection<ContractServiceLocation> contractServiceLocation;
}
Run Code Online (Sandbox Code Playgroud)
ContractServiceLocationPK
@Embeddable
public class ContractServiceLocationPK implements Serializable {
private long contractId;
private long locationId;
private long serviceId;
}
Run Code Online (Sandbox Code Playgroud)
ContractServiceLocation
@Entity
@Table(name="Contract_Service_Location")
public class ContractServiceLocation implements Serializable {
@EmbeddedId
ContractServiceLocationPK id;
@ManyToOne(cascade = CascadeType.ALL)
@MapsId("contractId")
Contract contract;
@ManyToOne(cascade = CascadeType.ALL) …Run Code Online (Sandbox Code Playgroud) 我正在研究JPA项目.我需要在具有三个主键的类上使用@OneToMany映射.您可以在此之后找到错误和类.
javax.persistence.PersistenceException: No Persistence provider for EntityManager named JTA_pacePersistence: Provider named oracle.toplink.essentials.PersistenceProvider threw unexpected exception at create EntityManagerFactory:
javax.persistence.PersistenceException
javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.EntityManagerSetupException
Exception Description: predeploy for PersistenceUnit [JTA_pacePersistence] failed.
Internal Exception: Exception [TOPLINK-7220] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [private java.util.Set isd.pacepersistence.common.Action.permissions] from the entity class [class isd.pacepersistence.common.Action] is incomplete. When the source entity class uses a composite primary key, …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据product_item表中的productId和toolboxItemId选择product_item_sortorder表中的匹配行.
在适用于给定productId的普通SQL中:
SELECT pi.*, pis.* FROM product_item pi
LEFT JOIN product_item_sortorder pis
ON pi.productId = pis.productId
AND pi.toolboxItemId = pis.toolboxItemId
WHERE pi.productId = 6
Run Code Online (Sandbox Code Playgroud)
我为它写了DQL如下:
$this->_em->createQuery(
'SELECT pi
FROM Entities\ProductItem pi
LEFT JOIN pi.sequence s
WHERE pi.product = ?1'
);
Run Code Online (Sandbox Code Playgroud)
如果输出$ query-> getSQL(),我会得到以下SQL:
SELECT p0_.id AS id0, p0_.productId AS productId1, p0_.priceGroupId AS priceGroupId2, p0_.toolboxItemId AS toolboxItemId3, p0_.levelId AS levelId4, p0_.parentId AS parentId5, p0_.productId AS productId6, p0_.toolboxItemId AS toolboxItemId7 FROM product_item p0_ LEFT JOIN product_item_sortorder p1_ ON p0_.productId = p1_. AND …Run Code Online (Sandbox Code Playgroud) 我有两个具有外键关系的表。我试过搜索如何做到这一点,它总是导致 OneToMany 和 ManyToOne 映射。我有这两张桌子。
用户角色
用户
我试图通过将位置列的值获取到 user_role 表来显示所有用户并显示他们的位置。
这些是我的课
用户.java
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
..........
private UserRole userRole;
@ManyToOne()
@JoinColumn(name="role_id")
public UserRole getUserRole() {
return userRole;
}
public void setUserRole(UserRole userRole) {
this.userRole = userRole;
}
.......
}
Run Code Online (Sandbox Code Playgroud)
用户角色.java
@Entity
@Table(name="user_role")
public class UserRole {
.......
private List<User> user;
@OneToMany(targetEntity=User.class, mappedBy="userRole",cascade=CascadeType.ALL, fetch = FetchType.LAZY)
public List<User> getUser() {
return user;
}
public void setUser(List<User> user) {
this.user = user; …Run Code Online (Sandbox Code Playgroud) many-to-one ×10
hibernate ×4
jpa ×4
one-to-many ×4
doctrine-orm ×3
java ×2
python ×2
dql ×1
lazy-loading ×1
left-join ×1
mapping ×1
maven-2 ×1
openerp ×1
persistence ×1
primary-key ×1
spring ×1
symfony ×1
xml ×1